简体   繁体   English

根据多属性选择查找选定的单选按钮值

[英]Find Selected Radio Button Value Based on Multiple Attribute Selection

I have 3 different Radio Buttons in a page with the following code 我在页面中有以下代码的3个不同的单选按钮

<input type="radio" name"1" value="1">1</input>

<input type="radio" name"2" value="2">2</input>

<input type="radio" name"3" value="3">3</input>

How can I get the value of the selected radio button with different names ? 如何获得具有不同名称的所选单选按钮的值?

I tried 我试过了

var option = $("input[type='radio'][name='1']:checked").val();

But giving Undefined. 但是给未定义。 Any Idea's ? 有任何想法吗 ?

You need to have them be the same name, otherwise they don't work as radios (they could all be selected), and you need your html to be valid: 您需要使它们具有相同的名称,否则它们不能用作无线电(它们都可以被选中),并且您需要HTML有效:

 <input type="radio" name="1" value="1">1
 <input type="radio" name="1" value="2">2
 <input type="radio" name="1" value="3">3

You're missing the = when assigning the name attribute. 分配name属性时,您将缺少=

<input type="radio" name="1" value="1" />
                  <!--  ^

Also, as others have pointed out in comments, input tags are self-closing. 同样,正如其他人在评论中指出的那样, input标签是自动关闭的。 (although it does work even with invalid html) (尽管即使使用无效的html也可以使用)

Demo: http://jsfiddle.net/g7RT2/ 演示: http//jsfiddle.net/g7RT2/

您在“ name”属性之后错过了“ =”符号,选择器与name = 1条件不匹配: <input type="radio" name"1" value="1">1</input> http:// jsfiddle .net / gLgBj /

here is a working fiddle 这是一个工作的小提琴

http://jsfiddle.net/mW7mk/ http://jsfiddle.net/mW7mk/

your html is not well formated, the input is self closed and name="1" not name"1" 您的html格式不正确,输入已自动关闭,并且name =“ 1”而非name“ 1”

   var options = $("input[type='radio'][name='1']:checked").val();
   alert("test : " + options);

worked just fine with : 可以正常工作:

   <label><input type="radio" name="1" value="1" checked="checked"/>1</label>
   <label><input type="radio" name="2" value="2"/>2</label>
   <label><input type="radio" name="3" value="3"/>3</label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM