php jquery - 选择所有带有 js 数组名称的复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2340806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery - select all checkboxes with js array name
提问by Pierre
I want to use a JQuery "check all" function in a form like this: http://jetlogs.org/jquery/jquery_select_all.html
我想以这样的形式使用 JQuery“全部检查”功能:http: //jetlogs.org/jquery/jquery_select_all.html
My problem is I am generating my form from a php script, and I don't know exactly how many checkboxes I will have in advance. I use the "array" naming convention to be able to get all the selected checkbox values in my $_POST data... so my checkboxes are like that:
我的问题是我正在从一个 php 脚本生成我的表单,我不知道我提前有多少个复选框。我使用“数组”命名约定能够在我的 $_POST 数据中获取所有选定的复选框值......所以我的复选框是这样的:
<input type="checkbox" name="items[]" value="<?php echo $id ?>">
but this JQuery declaration does not work:
但是这个 JQuery 声明不起作用:
$("input[@name=items[]]").each(function()
{
this.checked = checked_status;
});
probably because the "[]" at the end of my "items" messes up the JQuery declaration... I tried to write @name=items[] like that: "@name=items[]", and to put some anti-slash before the [ and ] so they're not considered as special characters but it didnt work...
可能是因为我的“items”末尾的“[]”弄乱了 JQuery 声明......我试图这样写@name=items[]:“@name=items[]”,并放一些反- 在 [ 和 ] 之前加上斜线,所以它们不被视为特殊字符,但它不起作用......
If someone knows a solution and is willing to share it'd be great!! :)
如果有人知道解决方案并愿意分享,那就太好了!!:)
回答by rochal
Escape internal brackets with \\(no space) - it should fix the problem.
使用\\(无空格)转义内部括号- 它应该可以解决问题。
This selector works for me:
这个选择器对我有用:
$('input[name=items\[\]]')
回答by Emil Ivanov
Try using
尝试使用
$('input[name="items[]"]')
No need for @and use ". Note that the selector outside quotes are '. Otherwise you might cause parse error.
不需要@和使用". 请注意,引号外的选择器是'. 否则可能会导致解析错误。
回答by Kennethvr
First of all, your name attribute should be used to set +1 elements with the same name. It's practically the same as the id attribute and should be unique per element excepted for references, but that is not the case for you.
首先,您的 name 属性应该用于设置具有相同名称的 +1 元素。它实际上与 id 属性相同,并且除了引用之外每个元素都应该是唯一的,但对您来说情况并非如此。
Try using the class attribute, it's made for the things you want to do!
尝试使用 class 属性,它是为您想做的事情而设计的!
another thing is that in your code, you can only set your checkboxes to 'checked', but never to uncheck, this code will set your checkboxes to checked true or false, according to what it gets as attribute from the clicked element.
另一件事是,在您的代码中,您只能将复选框设置为“已选中”,但永远不能取消选中,此代码会将您的复选框设置为已选中的 true 或 false,具体取决于它从被点击的元素中获取的属性。
you can do something like this:
你可以这样做:
set your check all checkbox with an id
使用 id 设置检查所有复选框
<input type="checkbox" id="checkAll">
Then set on all checkboxes that should be checked/unchecked a class
然后在所有应该被选中/取消选中的复选框上设置一个类
<input type="checkbox" class="checked">
And the you can do something like this in jQuery
你可以在 jQuery 中做这样的事情
$("#checkAll").click( function(){
var checkedValue = $(this).attr("checked");
$("input.checked").attr("checked", checkedValue); });
this will change all checkboxes with class checked to the same checked attribute as the one with id checkAll
这会将所有选中类的复选框更改为与 ID 为 checkAll 的复选框相同的选中属性
回答by Kannan Prasad
$("input[name='smsid[]']")
This code works fine for me...
这段代码对我来说很好用...

