[英]Get group radio button selected value via jquery
How can I get the selected radio button value via jQuery? 如何通过jQuery获取选定的单选按钮值? I tried:
我试过了:
var selectedPlan = $("input[name=Plan[packageId]]:checked'").val();
alert(selectedPlan);
bBt didn't work. bBt无效。
<td style="vertical-align: top;">
<input type="radio" value="1" name="Plan[packageId]">
<lable>3 Games</lable><br>
<input type="radio" value="2" name="Plan[packageId]">
<lable>5 Games</lable><br>
<input type="radio" value="3" name="Plan[packageId]">
<lable>10 Games</lable><br>
</td>
here is the fiddle http://jsfiddle.net/vk3z45an/ 这是小提琴http://jsfiddle.net/vk3z45an/
You need to escape the []
characters in the name attribute of the selector (or wrap them in quotes) and remove the un-matched apostrophe at the end. 您需要对选择器的name属性中的
[]
字符进行转义(或将它们用引号引起来),并在末尾删除不匹配的撇号。 Try this: 尝试这个:
var selectedPlan = $("input[name=Plan\\[packageId\\]]:checked").val();
alert(selectedPlan);
// alternatively:
// var selectedPlan = $("input[name='Plan[packageId]']:checked").val();
Note that you're running your code on load of the page and your HTML has no radio selected by default. 请注意,您是在页面加载时运行代码的,默认情况下,HTML未选中任何单选按钮。 I added a
checked
attribute to one of the radios so you can see it working. 我将其中一个
checked
属性添加到其中一个收音机中,以便可以看到它的正常运行。 Also, your original fiddle did not include jQuery so I added that too. 另外,您原来的小提琴不包含jQuery,所以我也添加了它。 Finally, the element is
label
, not lable
. 最后,元素是
label
,不是lable
。
You need to escape the CSS meta characters or wrap them in quote for preventing the selector from breaking: 您需要转义CSS元字符或将它们用引号引起来,以防止选择器损坏:
var selectedPlan = $("input[name='Plan[packageId]']:checked").val();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.