本文翻譯自: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"]')