<table>
<tr>
<td>
<div>
<input type="checkbox" id="home1">Home1</input>
<input type="checkbox" id="home2">Home3</input>
<input type="checkbox" id="home3">Home3</input>
<input type="checkbox" id="home4">Home4</input>
</div>
</td>
<td id="selectedhome"> </td>
</tr>
</table>
<input type="button" value="Select" />
在上面的代码中,单击按钮时,复选框的所有选定值都应显示在 selectedhome td 中,旁边有一个撤消链接,并且从第一个 td 中不可见。选择撤消按钮时,复选框应返回到初始位置。如何使用 jquery 实现此目的
最佳答案
尝试一下。它将把每个复选框重置为页面加载时的默认选中值。
示例: http://jsfiddle.net/6kFMk/1/
// Display ID value of checked boxes
$('input[value=Select]').click(function() {
var values = $('table :checkbox:checked').map(function() {
return this.id;
}).get().join(',');
var home = $('#selectedhome').text(values);
});
// Reset checkboxes to initial state
$('input[value=Undo]').click(function() {
$('table :checkbox').each(function() {
this.checked = this.defaultChecked;
});
});
关于jQuery 复选框撤消解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3746838/