本文翻译自: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的名字吗?
参考:https://stackoom.com/question/ecDd/如何在jQuery中选择具有name属性的元素-重复
jQuery supports CSS3 style selectors, plus some more. jQuery支持CSS3样式选择器,还有更多。
虽然您应该避免它,并且如果可能的话选择ID(例如#myId ),因为它具有更好的性能,因为它调用本机getElementById 。
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 baseit's very simple getting a name: 得到一个名字非常简单:
$('[name=elementname]');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 - 第一个结果)
你总是可以做$('input[name="somename"]')