How do I select a collection of checkboxes...?
I have the html:
<tr>
<td><input type="checkbox" value="3" id="userListingSelect0" cssclass="cbDelete">
</td><td>开发者_Python百科general</td>
<td><a href="editUser.aspx?userId=3">Edit</a></td>
</tr>
<tr>
<td><input type="checkbox" value="1" id="userListingSelect1" disabled="disabled" cssclass="cbDelete">
</td><td>hmladmin</td>
<td><a href="editUser.aspx?userId=1">Edit</a></td>
</tr>
And im trying to use jQuery to get all of the checkboxes.
Im using:
console.log(jQuery('.cbDelete').html());
But all im getting is undefined. How do I...
1) Get all the elements 2) Iterate through them?
I tried using:
jQuery('.cbDelete').each(function () {
console.log('got here');
if (this.checked) {
selectionMade = true;
}
resultsGot = true;
});
And still it didn't get to to the 'got here' line so it looks like I cant get to collection. Not sure why....
The reason why this line doesn't work is because of the cssclass
attribute
console.log(jQuery('.cbDelete').html());
Changing it to class
will make this work. Or the answer from Igor.
jQuery class selector works when class
attribute is defined, but you have cssclass
. So in your case you may try:
$("input[cssclass='cbDelete']");
To iterate through just use each
as usual.
You're using a non-standard identifier to give the checkbox a class. Change cssclass
to class
and then $('.cbDelete')
will work; the .
(dot) means "class".
As @Igor Dymov says rather abruptly in his answer, using
$("input[cssclass='cbDelete']");
works; this finds any input with the custom attribute cssclass
, with a value of cbDelete
.
Personally, I'd use a class as it's cleaner, validates well and the selector is simpler (and possibly a bit faster).
$("input:checkbox").each(function(){
console.log('got here');
if (this.checked) {
selectionMade = true;
}
resultsGot = true;
});
If you want to retrieve only those checkbox inside your specified table(let's say your table's id is "table_a"), it will be
$("#table_a input:checkbox").each(function(){
Yeah if the cssClass is just changed to class it works. So your view does not change the cssClass to class? Or was that in source? Some people gave you already answer for if you are using the cssClass in source.
If you are defining that in asp:
<asp:TextBox id="TextBox1" ForeColor="Red" CssClass="class1" />
should render to
<input type=text class="class1" style="ForeColor:red">
http://jsfiddle.net/SdHuK/
精彩评论