Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

javascript - 在jQuery中,如何通过其name属性选择元素?(In jQuery, how do I select an element by its name attribute?)

I have 3 radio buttons in my web page, like below:(我的网页中有3个单选按钮,如下所示:)

<label for="theme-grey"> <input type="radio" id="theme-grey" name="theme" value="grey" />Grey</label> <label for="theme-pink"> <input type="radio" id="theme-pink" name="theme" value="pink" />Pink</label> <label for="theme-green"> <input type="radio" id="theme-green" name="theme" value="green" />Green</label> In jQuery, I want to get the value of the selected radio button when any of these three are clicked.(在jQuery中,我想在单击这三个按钮中的任何一个时获取所选单选按钮的值。) In jQuery we have id (#) and class (.) selectors, but what if I want to find a radio button by its name, as below?(在jQuery中,我们具有id(#)和class(。)选择器,但是如果我想按名称查找单选按钮,该怎么办呢?) $("<radiobutton name attribute>").click(function(){}); Please tell me how to solve this problem.(请告诉我如何解决这个问题。)   ask by Prashant translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should do it, all of this is in the documentation , which has a very similar example to this:(应该做到这一点,所有这些都在文档中 ,该文档具有与此类似的示例:)

$("input[type='radio'][name='theme']").click(function() { var value = $(this).val(); }); I should also note you have multiple identical IDs in that snippet.(我还应该注意,您在该代码段中有多个相同的ID。) This is invalid HTML.(这是无效的HTML。) Use classes to group set of elements, not IDs, as they should be unique.(使用类将元素集而不是ID分组,因为它们应该是唯一的。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...