jquery - 选择所有复选框与js数组名称 [英] jquery - select all checkboxes with js array name

查看:171
本文介绍了jquery - 选择所有复选框与js数组名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这样的表单中使用JQuerycheck all函数:
http://jetlogs.org/jquery/jquery_select_all.html



我的问题是我从PHP脚本生成我的表单,我不知道我将提前多少个复选框。
我使用数组命名约定能够获得我的$ _POST数据中所有选中的复选框值...
所以我的复选框是这样:

 < input type =checkboxname =items []value =<?php echo $ id?>> 

但此JQuery声明不起作用:

  $(input [@ name = items []])。each(function()
{
this.checked = checked_status;
});

可能是因为我的items末尾的[]混乱了JQuery声明。我试图写@ name = items []像这样:@ name = items [],并在[和]之前放一些反斜杠,所以它们不被认为是特殊字符,但它did not工作。 ..



如果有人知道一个解决方案,并愿意分享它将是伟大的! :)

解决方案

使用 \\

 <$>这个选择器适用于我: $('input [name = items\\ [\\]]')


I want to use a JQuery "check all" function in a form like this: 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:

<input type="checkbox" name="items[]" value="<?php echo $id ?>">

but this JQuery declaration does not work:

$("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...

If someone knows a solution and is willing to share it'd be great!! :)

解决方案

Escape internal brackets with \\(no space) - it should fix the problem.

This selector works for me:

$('input[name=items\\[\\]]')

这篇关于jquery - 选择所有复选框与js数组名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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