当我单击按钮时,此代码应清除复选框。如果我删除 <form></form>
它会起作用标签,但我认为 .find()
应该找到所有后代?
<script type="text/javascript">
$(document).ready(function(){
var clearCheckboxes = function() {
$('.outerbox').find('input').each(function() {
$(this).attr('checked', false);
});
}
$('input.myButton').click(clearCheckboxes);
});
</script>
<div class="outerbox">
<form>
<input type="checkbox" checked="" /> checkbox1
<input type="checkbox" checked="" /> checkbox2
</form>
</div>
<input class="myButton" value="clear checkboxes now" type="button"/>
最佳答案
这段代码对我来说很好用:http://jsfiddle.net/CgsEu/
无论如何,如果您使用的是最新的 jQuery,请尝试将 .attr
更改为 .prop
。也不需要 .each
。 .attr
和 .prop
适用于 jQuery 对象中的所有元素。
var clearCheckboxes = function() {
$('.outerbox').find('input').prop('checked', false)
}
演示:http://jsfiddle.net/CgsEu/1/
如果还有其他输入,请尝试将 .find
限制为仅复选框。
var clearCheckboxes = function() {
$('.outerbox').find('input:checkbox').prop('checked', false)
}
关于javascript - jquery .find() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10386396/