admin管理员组文章数量:1415100
This code should clear the checkboxes when I click the button. It works if I remove the <form></form>
tags, but I thought .find()
was supposed to find all descendants?
<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"/>
This code should clear the checkboxes when I click the button. It works if I remove the <form></form>
tags, but I thought .find()
was supposed to find all descendants?
<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"/>
Share
Improve this question
asked Apr 30, 2012 at 15:39
JYXJYX
2,6632 gold badges19 silver badges15 bronze badges
10
- 3 Try using .prop instead of .attr – kinakuta Commented Apr 30, 2012 at 15:41
- 2 It's working fine apparently - jsfiddle/gYHKA – Nikhil Baliga Commented Apr 30, 2012 at 15:42
- 1 Works for me: jsfiddle/CgsEu – gen_Eric Commented Apr 30, 2012 at 15:42
- 2 This jsFiddle has your code in it and it seems to work fine. Doesn't seem to be a jQuery issue, maybe the browser you are using. jsfiddle/aC6g2 – Michael Allen Commented Apr 30, 2012 at 15:43
- 1 yes :) would be a good question though. Actually, does it have anything to do with the fact that I'm trying to call this code on .submit rather than .click? – JYX Commented Apr 30, 2012 at 15:49
5 Answers
Reset to default 2This code works fine for me: http://jsfiddle/CgsEu/
Anyway, if you are using the latest jQuery, try changing .attr
to .prop
. Also the .each
isn't needed. .attr
and .prop
work on all elements in a jQuery object.
var clearCheckboxes = function() {
$('.outerbox').find('input').prop('checked', false)
}
DEMO: http://jsfiddle/CgsEu/1/
If there are other inputs, try limiting the .find
to just checkboxes.
var clearCheckboxes = function() {
$('.outerbox').find('input:checkbox').prop('checked', false)
}
DEMO: http://jsfiddle/CgsEu/2/
$(document).ready(function(){
var clearCheckboxes = function() {
$('.outerbox').find('input[type="checkbox"]').each(function(){
$(this).prop('checked', false);
});
}
$('input.myButton').click(clearCheckboxes);
});
DEMO.
Update:
$('.outerbox').find('input[type="checkbox"]').prop('checked', false);
or
$('.outerbox input:checkbox').prop('checked', false);
DEMO.
There's no need to use each(), you already have a collection of the elements and can apply the change to all of them, like so:
var clearCheckboxes = function() {
$('input', '.outerbox').attr('checked', false);
}
$('input.myButton').click(clearCheckboxes);
FIDDLE
There are a lot of suggestions to use prop() over attr(), and that is probably sound advice.
According to the W3C forms specification, the checked attribute is a boolean attribute, which means the corresponding property is true if the attribute is present at all—even if, for example, the attribute has no value or an empty string value. The preferred cross-browser-patible way to determine if a checkbox is checked is to check for a "truthy" value on the element's property using one of the following:
if ( elem.checked )
if ( $(elem).prop("checked") )
if ( $(elem).is(":checked") )
To maintain backwards patability, the .attr() method in jQuery 1.6.1+ will retrieve and update the property for you so no code for boolean attributes is required to be changed to .prop(). Nevertheless, the preferred way to retrieve a checked value is prop().
Use prop, e.g.
$(this).prop('checked', false);
instead if attr
var clearCheckboxes = function() {
$('input[type="checkbox"]', '.outerbox').prop('checked', false);
}
$('input.myButton').click(clearCheckboxes);
本文标签: javascriptjquery find() not workingStack Overflow
版权声明:本文标题:javascript - jquery .find() not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745161711a2645469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论