jQuery - 添加/删除表格行
根据我在右侧的选择,我动态地构造(克隆)左侧的一组行。我还添加了一个“删除”按钮。我无法从该按钮调用 jQuery 函数。另外,单击“删除”按钮后,如何将右侧相应行的复选框设置为之前的状态(选中)。下面有全部代码。 谢谢。
例子
<script type="text/javascript">
$(document).ready(function(){
$("#row_remove").click(function() {
alert("1");
});
$('#tb11 tr')
.filter(':has(:checkbox:checked)')
.addClass('selected')
.end()
.click(function(event) {
//alert("1");
$(this).toggleClass('selected');
if (event.target.type !== 'checkbox') {
$(':checkbox', this).attr('checked', function() {
if ($(this).is(":checked"))
{
$(this).closest("tr").clone().appendTo("#tbl2").append("<label><input type='text' name='textfield' id='textfield'/></label><button id='row_remove'>remove</button>");
//ac 1
var findCheckBox = $("#tbl2 input:checkbox.chkclass");
findCheckBox.remove();
}
else
{
var index = $(this).closest("tr").attr("id");
var findRow = $("#tbl2 tr[id='" + index + "']");
findRow.remove();
}
return !this.checked;
});
}
});
});
</script>
<style type="text/css">
table {
text-align: center;
}
.right {
text-align: left;
}
.right {
text-align: left;
}
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<table width="100%" border="0" cellpadding="4">
<tr>
<td width="32%" rowspan="3" valign="top" bgcolor="#EAEAEA">
<table width="100%" border="0">
<tr id="tbl2" width="100%">
<!-- <td height="101" valign="top" bgcolor="#FFFFFF" class="right"> </td>-->
</tr>
</table></td>
<td width="1%" bgcolor="#EAEAEA"> </td>
<td width="67%" colspan="3" rowspan="3" valign="top" bgcolor="#EAEAEA">
<table id="tb11" width="100%" border="0">
<tbody>
<tr id="1">
<td width="5%" height="20" valign="top" bgcolor="#FFFFFF"><input type="checkbox" class="chkclass" value="yes" checked="checked" /></td>
<td width="95%" valign="top" bgcolor="#FFFFFF" class="right">item 1</td>
</tr>
<tr id="2">
<td width="5%" height="20" valign="top" bgcolor="#FFFFFF"><input type="checkbox" class="chkclass" value="yes" checked="checked" /></td>
<td width="95%" height="20" valign="top" bgcolor="#FFFFFF" class="right">item 2</td>
</tr>
<tr id="3">
<td width="5%" height="20" valign="top" bgcolor="#FFFFFF"><input type="checkbox" class="chkclass" value="yes" checked="checked" /></td>
<td width="95%" height="20" valign="top" bgcolor="#FFFFFF" class="right">item 3</td>
</tr>
<tr id="4">
<td width="5%" height="20" valign="top" bgcolor="#FFFFFF"><input type="checkbox" class="chkclass" value="yes" checked="checked" /></td>
<td width="95%" height="20" valign="top" bgcolor="#FFFFFF" class="right">item 4</td>
</tr>
<tr id="5">
<td width="5%" height="20" valign="top" bgcolor="#FFFFFF"><input type="checkbox" class="chkclass" value="yes" checked="checked" /></td>
<td width="95%" height="20" valign="top" bgcolor="#FFFFFF" class="right">item 5</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td bgcolor="#EAEAEA"> </td>
</tr>
<tr>
<td bgcolor="#EAEAEA"> </td>
</tr>
</table>
</body>
</html>
From my selection on the right I dynamically construct (clone) a set of rows to the left. There I also add a REMOVE button. I can't call a jQuery function from that button. Also upon clicking the REMOVE button how can I set the checkbox of the respective row on the right to its previous state (checked). There is all the code below.
Thank you.
Example
</script>
<script type="text/javascript">
$(document).ready(function(){
$("#row_remove").click(function() {
alert("1");
});
$('#tb11 tr')
.filter(':has(:checkbox:checked)')
.addClass('selected')
.end()
.click(function(event) {
//alert("1");
$(this).toggleClass('selected');
if (event.target.type !== 'checkbox') {
$(':checkbox', this).attr('checked', function() {
if ($(this).is(":checked"))
{
$(this).closest("tr").clone().appendTo("#tbl2").append("<label><input type='text' name='textfield' id='textfield'/></label><button id='row_remove'>remove</button>");
//ac 1
var findCheckBox = $("#tbl2 input:checkbox.chkclass");
findCheckBox.remove();
}
else
{
var index = $(this).closest("tr").attr("id");
var findRow = $("#tbl2 tr[id='" + index + "']");
findRow.remove();
}
return !this.checked;
});
}
});
});
</script>
<style type="text/css">
table {
text-align: center;
}
.right {
text-align: left;
}
.right {
text-align: left;
}
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<table width="100%" border="0" cellpadding="4">
<tr>
<td width="32%" rowspan="3" valign="top" bgcolor="#EAEAEA">
<table width="100%" border="0">
<tr id="tbl2" width="100%">
<!-- <td height="101" valign="top" bgcolor="#FFFFFF" class="right"> </td>-->
</tr>
</table></td>
<td width="1%" bgcolor="#EAEAEA"> </td>
<td width="67%" colspan="3" rowspan="3" valign="top" bgcolor="#EAEAEA">
<table id="tb11" width="100%" border="0">
<tbody>
<tr id="1">
<td width="5%" height="20" valign="top" bgcolor="#FFFFFF"><input type="checkbox" class="chkclass" value="yes" checked="checked" /></td>
<td width="95%" valign="top" bgcolor="#FFFFFF" class="right">item 1</td>
</tr>
<tr id="2">
<td width="5%" height="20" valign="top" bgcolor="#FFFFFF"><input type="checkbox" class="chkclass" value="yes" checked="checked" /></td>
<td width="95%" height="20" valign="top" bgcolor="#FFFFFF" class="right">item 2</td>
</tr>
<tr id="3">
<td width="5%" height="20" valign="top" bgcolor="#FFFFFF"><input type="checkbox" class="chkclass" value="yes" checked="checked" /></td>
<td width="95%" height="20" valign="top" bgcolor="#FFFFFF" class="right">item 3</td>
</tr>
<tr id="4">
<td width="5%" height="20" valign="top" bgcolor="#FFFFFF"><input type="checkbox" class="chkclass" value="yes" checked="checked" /></td>
<td width="95%" height="20" valign="top" bgcolor="#FFFFFF" class="right">item 4</td>
</tr>
<tr id="5">
<td width="5%" height="20" valign="top" bgcolor="#FFFFFF"><input type="checkbox" class="chkclass" value="yes" checked="checked" /></td>
<td width="95%" height="20" valign="top" bgcolor="#FFFFFF" class="right">item 5</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td bgcolor="#EAEAEA"> </td>
</tr>
<tr>
<td bgcolor="#EAEAEA"> </td>
</tr>
</table>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
元素ID属性值必须是唯一的!永远记住这一点!
查看 clone() 文档。此方法有可选参数“withDataAndEvents”,默认情况下为 false。
Element ID attribute value must be unique!!! Remember this forever!
Check out clone() docs. This method has optional parameter "withDataAndEvents" which is false by default.