【发布时间】:2017-11-18 10:49:06
【问题描述】:
我正在处理申请表。除了 PI 和表格的提交者之外,它还需要列出研究研究的研究团队成员。但是,有些研究将没有其他团队成员,因此我希望该行保持隐藏状态,直到有人单击“添加团队成员”按钮。
什么有效: 1. 我在最初加载页面时隐藏了该元素。 2. 单击添加行添加正确的行。 3. 点击remove会删除一行。
当前问题: 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="you@example.com" />
</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>
【问题讨论】:
标签: javascript jquery html