开发者

How to select all input with given name if name contains [ and ]?

开发者 https://www.devze.com 2023-04-03 01:45 出处:网络
How to select all input with given name if name contains [ and ]? $(\'input[name=field_name[array_key]]\').removeClass(\'select开发者_如何学Goed\');

How to select all input with given name if name contains [ and ]?

$('input[name=field_name[array_key]]').removeClass('select开发者_如何学Goed');

Console:

Uncaught Syntax error, unrecognized expression: [name=field_name[array_key]]

Thank you for answers!


$('input[name=field_name\\[array_key\\]]').removeClass('selected');


escape the special characters like [] characters

$('input[name=field_name\\[array_key\\]]').removeClass('selected');


You need to escape with "\\"

From Jquery docs:

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")


Put the name value in quotes:

$('input[name="field_name[array_key]"]').removeClass('selected');

As stated by the jQuery docs:

Quotes are mandatory

Here's a live example of the above code.

0

精彩评论

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