我正在處理請求表單。它需要列出研究團隊成員除了PI和提交表格之外的研究。然而,有些研究沒有額外的團隊成員,所以我希望該行保持隱藏狀態,直到有人點擊添加團隊成員按鈕。最初隱藏然後動態添加和刪除行
什麼是工作: 1.我已經隱藏最初加載頁面上的元素。 2.單擊添加行添加正確的行。 3.單擊刪除將刪除一行。
當前問題: 1.如果有人添加了團隊成員,然後刪除所有團隊成員,單擊添加團隊成員將不會添加一行。 2.當元素在初始頁面加載時隱藏時,第一次點擊添加團隊成員按鈕時會添加兩行。
這是我現在的代碼,只有表格的相關部分。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="assets/css/test.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function addTableRow(jQtable){
jQtable.each(function(){
var tds = '<tr>';
jQuery.each($('tr:last td', this), function() {tds += '<td>'+$(this).html()+'</td>';});
tds += '</tr>';
if($('tbody', this).length > 0){$('tbody', this).append(tds);
}else {$(this).append(tds);}
});
}
</script>
<script>
function myDeleteFunction() {
document.getElementById("stmember").deleteRow(0);
}
</script>
<script type="text/javascript">
$(function() {
$('#add').click(function() {
$('#stmember').show();
});
});
</script>
<style>
#stmember {
display: none
}
</style>
</head>
<body>
<h3><strong>Other Study Team Members:</strong></h3>
<FORM>
<table id="stmember">
<tr>
<td>Name:
<label for="namest1"></label>
<input type="text" name="namest1" id="namest1" placeholder="First Name, Last Name" />
</td>
<td>JHED ID:
<label for="jhedst1"></label>
<input type="text" name="jhedst1" id="jhedst1" />
</td>
<td>Email:
<label for="emailst1"></label>
<input type="email" name="emailst1" id="emailst1" placeholder="[email protected]" />
</td>
</tr>
</table>
<CENTER>
<button type="button" id="add" onclick="addTableRow($('#stmember'));">Add Study Team Member</button>
<button type="button" onclick="myDeleteFunction()">Remove Study Team Member</button>
</CENTER>
</FORM>
</body>
</HTML>