[英]How to select all input with given name if name contains [ and ]?
How to select all input with given name if name contains [ and ]? 如果名称包含[和],如何选择所有具有给定名称的输入?
$('input[name=field_name[array_key]]').removeClass('selected');
Console: 安慰:
Uncaught Syntax error, unrecognized expression: [name=field_name[array_key]]
未捕获的语法错误,无法识别的表达式:[name = field_name [array_key]]
Thank you for answers! 谢谢您的回答!
$('input[name=field_name\\[array_key\\]]').removeClass('selected');
转义[]
等特殊字符
$('input[name=field_name\\[array_key\\]]').removeClass('selected');
You need to escape with " \\\\
" 您需要使用“
\\\\
”转义
From Jquery docs: 从Jquery文档:
If you wish to use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, you must escape the character with two backslashes: \\. For example, if you have an element with id="foo.bar", you can use the selector $("#foo\\.bar")
如果您希望使用任何元字符(如!“#$%&'()* +,。/ :; <=>?@ [] ^`{|}〜)作为a的原义部分,名称,必须用两个反斜杠转义字符:\\。例如,如果您的元素具有id =“ foo.bar”,则可以使用选择器$(“#foo \\ .bar”)
Put the name
value in quotes: 将
name
值放在引号中:
$('input[name="field_name[array_key]"]').removeClass('selected');
As stated by the jQuery docs : 如jQuery docs所述 :
Quotes are mandatory
引号是强制性的
Here's a live example of the above code. 这是上述代码的实时示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.