How to Select all Checkboxes with jQuery
I need to "check" all of the checkboxes in an asp.net gridview. I 开发者_如何学编程can do so with code-behind but would rather do this on the client side with jquery. The column that the checkbox is stored in is called "displayButton" because it displays another action button once a checkbox is checked.
Not sure how to begin here.
You would start with a selector that selects all the checkboxes that you want to check, then use the prop()
function to change their 'checked' properties to true:
$('input:checkbox').not(':checked').prop('checked', true);
Note: Check mblase75's response for jQuery version issues.
--UPDATE--
I ran a jsperf and not checking if the checkbox is already checked is quite a lot slower (approximately 50% slower) than just using the .prop()
function to set all checkboxe's checked property to true.
$("input:checkbox").prop("checked",true)
or $("input:checkbox").attr("checked","checked")
for jQuery 1.5.x or earlier.
That said: you say you would rather not do this in ASP.Net, but if it's at all possible your users will have JavaScript turned off, you'll have to do it server-side.
for jquery lower than 1.6
$('input:checkbox').attr('checked', "checked");
and for 1.6+
$('input:checkbox').prop('checked', true);
$("#id input:checkbox").prop("checked", true);
or
$(".class input:checkbox").prop("checked", true);
$('#girdid input [type="checkbox"]').attr('checked', true);
this is solution. I hope it helps
精彩评论