我有两个单选按钮,希望发布所选按钮的值。如何使用jQuery获取值?

我可以得到所有这些:

$("form :radio")

我如何知道选择了哪一个?


当前回答

要检索JavaScript数组中的所有单选按钮值,请使用以下jQuery代码:

var values = jQuery('input:checkbox:checked.group1').map(function () {
    return this.value;
}).get();

其他回答

在我的例子中,我在一个表单中有两个单选按钮,我想知道每个按钮的状态。以下内容适用于我:

//获取单选按钮值console.log(“radio1:”+$('input[id=radio1]:checked','#toggle form').val());console.log(“radio2:”+$('input[id=radio2]:checked','#toggle form').val());<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script><form id=“toggle form”><div id=“radio”><input type=“radio”id=“radio1”name=“radio“checked=”checked“/><label for=”radio1“>绘制单个</label><input type=“radio”id=“radio2”name=“radio”/><label for=“radio2”>全部绘制</label></div></form>

下面是我如何填写表格并处理已检查的收音机的获取。

使用名为myForm的表单:

<form id='myForm'>
    <input type='radio' name='radio1' class='radio1' value='val1' />
    <input type='radio' name='radio1' class='radio1' value='val2' />
    ...
</form>

从表单中获取值:

$('#myForm .radio1:checked').val();

如果您不发布表单,我会通过以下方式进一步简化:

<input type='radio' class='radio1' value='val1' />
<input type='radio' class='radio1' value='val2' />

然后获取检查值变为:

    $('.radio1:checked').val();

在输入上有类名可以让我轻松地设置输入的样式。。。

试试这个。这对我有用

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

要获取id为myForm的表单的选定radioName项的值,请执行以下操作:

$('input[name=radioName]:checked', '#myForm').val()

下面是一个示例:

$('#myForm input').on('change',function(){alert($('input[name=radioName]:checked','#myForm').val());});<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script><form id=“myForm”><fieldset><legend>选择radioName</legend><label><input-type=“radio”name=“radioName”value=“1”/>1</label><br/><label><input-type=“radio”name=“radioName”value=“2”/>2</label><br/><label><input-type=“radio”name=“radioName”value=“3”/>3</label><br/></fieldset></form>