使用jQuery问题选择所有复选框并且不起作用 [英] Select all checkboxes using jquery issue and not working function

查看:53
本文介绍了使用jQuery问题选择所有复选框并且不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:如何选择具有指定ID的所有复选框?

First: How can I select all checkboxes with specified ID?

第二:

我得到了:

<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);
 });
});

alert并没有出现在我的屏幕上-表示该功能未运行-为什么会发生?

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

推荐答案

两件事:

  1. 没有选择器:all,并且您不能在ID中使用冒号-删除
  2. ID必须是唯一的.您不能有2个或多个具有相同ID的元素.
  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');
});

这篇关于使用jQuery问题选择所有复选框并且不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆