问题
I'm not sure if this is a bug or just some crazy new thing in jQuery 1.3 that I'm unaware of, or if I've just gone crazy.
I have a table with 11 checkboxes in it, and I can't select them all using jQuery 1.3:
// jQuery 1.2.6
$(".myTable").find(":checkbox"); // finds 11 elements
// jQuery 1.3
$(".myTable").find(":checkbox"); // finds 1 element: the first checkbox
$(":checkbox", $(".myTable")); // finds 1 element
$('.myTable :checkbox')); // finds all 11 elements
The results are the same if I use .find('*')
: it only picks the first element in 1.3, so it's nothing peculiar to :checkbox
.
On my own page, I can recreate this every time, but when I paste the (seemingly) relevant parts into JSBin, it works!
The original page also has Mootools included, but I've been very careful with scoping and there weren't any issues with jQ 1.2.6, so I don't think that could be it. Any other ideas?
And before anyone says it, using the .find()
function is very much more convenient than the combined selector (".myTable :checkbox"
) in this case, and changing all my code to that style isn't an option!
回答1:
If it's really a bug, you should visit the jQuery bug tracker site and report it (see http://dev.jquery.com/).
This is especially true as 1.3 has only just been released. However, given the amount of testing it's gone through, I'd strongly suggest you try a very simple web page to see if it's really a problem with jQuery or, as you suggest, a possible interaction with your other tools (i.e., Mootools). A bare bones page with just a couple of check boxes, the 1.3 jQuery and the code you've given in your question would be ideal.
Only if it's still an issue then would I raise a bug on jQuery, otherwise I'd start with the various discussion groups to see if they can help.
For example, this piece of code does actually work so it's unlikely to be a bug with jQuery.
<html>
<head>
<script type="text/javascript" src="jquery-1.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(document).find(":checkbox").attr('checked',false);
$("a").click(function(event){
$(".myTable").find(":checkbox").attr('checked',true);
event.preventDefault();
});
});
</script>
</head>
<body>
<a href="http://nowhere.com/">Click me!</a><hr>
<table class="myTable"><tr>
<td><input type="checkbox">One</input></td>
<td><input type="checkbox">Two</input></td>
<td><input type="checkbox">Three</input></td>
</tr></table>
</body>
</html>
来源:https://stackoverflow.com/questions/456457/jquery-1-3-only-selecting-the-first-element