jquery select element by name attribute

社会主义新天地 提交于 2019-12-13 04:36:16

问题


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.


回答1:


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

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



回答2:


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



回答3:


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

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




回答4:


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/



来源:https://stackoverflow.com/questions/6278423/jquery-select-element-by-name-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!