当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How do I select an element with its name attribute in jQuery?

问题描述:

This question already has an answer here:

  • How can I select an element by name with jQuery?

    13 answers

网友答案:
$('[name="ElementNameHere"]').doStuff();

jQuery supports CSS3 style selectors, plus some more.

See more

  • jQuery - Selectors
  • jQuery - [attribute=""] selector
网友答案:

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
网友答案:

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)

网友答案:
jQuery("[name='test']") 

Although you should avoid it and if possible select by ID (e.g. #myId) as this has better performance because it invokes the native getElementById.

网友答案:

You could always do $('input[name="somename"]')

分享给朋友:
您可能感兴趣的文章:
随机阅读: