jquery select element by name attribute

2019-09-03 12:55发布

I have an input with the name attribute:

<input type="text" name="data[foo][bar]" />

how can I select this element?

I tried $("input[name=data[foo][bar]]") but in vein.

4条回答
疯言疯语
2楼-- · 2019-09-03 13:11

Use

$("input[name=data\\[foo\\]\\[bar\\]]")

The documentation says:

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

http://api.jquery.com/category/selectors/

查看更多
女痞
3楼-- · 2019-09-03 13:17
$("input[name='data[foo][bar]']") 
查看更多
forever°为你锁心
4楼-- · 2019-09-03 13:25

Add quotes to the attribute value, otherwise you get conflicting square brackets and a parse error:

$("input[name='data[foo][bar]']")
查看更多
看我几分像从前
5楼-- · 2019-09-03 13:25
$('input[name="data[foo][bar]"]')

http://api.jquery.com/attribute-equals-selector/ for more info

查看更多
登录 后发表回答