开发者

Select all checkboxes using jquery issue and not working function

开发者 https://www.devze.com 2023-04-11 21:55 出处:网络
First: How can I select all che开发者_StackOverflowckboxes with specified ID? Second: I got: <a href=\"javascript://\" id=\"chkbox:all\">Click to select all of the checkboxes</a>

First: How can I select all che开发者_StackOverflowckboxes with specified ID?

Second:

I got:

<a href="javascript://" id="chkbox:all">Click to select all of the checkboxes</a>

And then at the top of the script I'm using this:

$(function () {
 $('#chkbox:all').click(function () {
  alert(1);
 });
});

And alert doesn't appear on my screen - mean that function is not running - why it happends?


If you're trying to set more than 1 checkbox with the same ID, you should be using a class instead, since IDs are supposed to be unique identifiers.

Also, don't use ":all" as part of your identifiers, since jQuery might think you want to use a selector... try to change that and your code should work :)


Two things:

  1. There's no selector :all and you can't use colons in IDs--take that out
  2. IDs must be unique. You can't have 2 or more elements with the same ID.

You'll need to use another strategy for finding all the checkboxes. Something like this:

$('input:checkbox').click( /* ... */ );

Or add a class to all the checkboxes and do this:

$('input.yourclass:checkbox').click( /* ... */ );

If you want to click on a link and have that check all the boxes, try this:

// check on all the checks
$('#all-link-id').click(function(){ 
  $('input.yourclass:checkbox').attr('checked', true);
});

// check off all the checks
$('#none-link-id').click(function(){ 
  $('input.yourclass:checkbox').removeAttr('checked');
});
0

精彩评论

暂无评论...
验证码 换一张
取 消