本文翻译自:How do I select an element with its name attribute in jQuery? [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
- How can I select an element by name with jQuery? 如何使用jQuery按名称选择元素? 14 answers 14个答案
How to get an element with its name
attribute with jQuery? 如何使用jQuery获取具有name
属性的元素?
Is there anything (like #
for id and .
for class) for name in jQuery? 有什么(如#
中的id和.
类)为jQuery的名字吗?
#1楼
参考:https://stackoom.com/question/ecDd/如何在jQuery中选择具有name属性的元素-重复
#2楼
$('[name="ElementNameHere"]').doStuff();
jQuery supports CSS3 style selectors, plus some more. jQuery支持CSS3样式选择器,还有更多。
See more 看更多
#3楼
jQuery("[name='test']")
虽然您应该避免它,并且如果可能的话选择ID(例如#myId
),因为它具有更好的性能,因为它调用本机getElementById
。
#4楼
You can use: 您可以使用:
jQuery('[name="' + nameAttributeValue + '"]');
this will be an inefficient way to select elements though, so it would be best to also use the tag name or restrict the search to a specific element: 这将是一种选择元素的低效方式,因此最好还使用标记名称或将搜索限制为特定元素:
jQuery('div[name="' + nameAttributeValue + '"]'); // with tag name
jQuery('div[name="' + nameAttributeValue + '"]',
document.getElementById('searcharea')); // with a search base
#5楼
it's very simple getting a name: 得到一个名字非常简单:
$('[name=elementname]');
Resource: 资源:
http://www.electrictoolbox.com/jquery-form-elements-by-name/ (google search: get element by name jQuery - first result) http://www.electrictoolbox.com/jquery-form-elements-by-name/ (谷歌搜索:按名称获取元素jQuery - 第一个结果)
#6楼
你总是可以做$('input[name="somename"]')