javascript 获取选中单选按钮的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11553908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the value of the selected radio button
提问by harsh
I have defined a set of radio buttons in HTML in a form as follows. The name of my form is "MyForm". When Female is selected, I want the value of a variable to be equal to "Female".
我在 HTML 中以如下形式定义了一组单选按钮。我的表单的名称是“MyForm”。选择女性时,我希望变量的值等于“女性”。
<fieldset data-role="controlgroup" data-type="horizontal" id="gender" name="gender">
<legend>Gender :</legend>
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked" />
<label for="radio-choice-1">Male</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2"/>
<label for="radio-choice-2">Female</label>
</fieldset>
I tried as var
我尝试作为 var
genderValue = $('input[name=gender]:checked', '#MyForm').val()
But this is not working. What is the correct way?
但这行不通。正确的方法是什么?
采纳答案by undefined
you can store the values(female
,...
), in value attribute of the inputs, based on your markup you can try the following:
您可以根据您的标记将值(female
,...
)存储在输入的值属性中,您可以尝试以下操作:
$('input[name="radio-choice-1"]').change(function(){
var genderValue = $(this).next('label').text()
})
但我建议这个:
<fieldset data-role="controlgroup" data-type="horizontal" id="gender" name="gender">
<legend>Gender :</legend>
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="Male" checked="checked" />
<label for="radio-choice-1">Male</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="Female"/>
<label for="radio-choice-2">Female</label>
</fieldset>
$('input[name="radio-choice-1"]').change(function(){
var genderValue = this.value
})
回答by gaurang171
Your issue has been solved as below:
您的问题已解决如下:
HTML:
HTML:
<form id="MyForm" name="MyForm" method="post">
<fieldset>
<legend>
Gender
</legend>
<div id="panel">
<input type="radio" name="radio-choice" id="radio-choice-1" value="choice-1" />
<label for="radio-choice-1">
Male
</label>
<input type="radio" name="radio-choice" id="radio-choice-2" value="choice-2" />
<label for="radio-choice-2">
Female
</label>
</div>
<div id="result">
</div>
</fieldset>
</form>
jQuery:
jQuery:
$(function() {
$('input[name="radio-choice"]').change(function() {
var genderValue = $(this).next('label').text();
$("#result").html("<p> You have selected " + genderValue + ".");
})
});
I have done bins on http://codebins.com/bin/4ldqpaa
我在http://codebins.com/bin/4ldqpaa上做过垃圾箱
回答by Sudhir
This might helpful to someone, Here is another way to retrieve selected radio button value
这可能对某人有帮助,这是检索选定单选按钮值的另一种方法
var gender = $('input[name="radio-choice-1"]:checked').val();
Setting id to radio buttons is optional here.
将 id 设置为单选按钮在这里是可选的。
Happy coding!....
快乐编码!...