简体   繁体   中英

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]]

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM