我有两个单选按钮,希望发布所选按钮的值。如何使用jQuery获取值?
我可以得到所有这些:
$("form :radio")
我如何知道选择了哪一个?
我有两个单选按钮,希望发布所选按钮的值。如何使用jQuery获取值?
我可以得到所有这些:
$("form :radio")
我如何知道选择了哪一个?
当前回答
获取所有收音机:
var radios = jQuery("input[type='radio']");
筛选以获取选中的
radios.filter(":checked")
其他回答
另一种获取方法:
$(“#myForm input[type=radio]”).on(“change”,function(){如果(选中此项){警报(this.value);}});<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script><form id=“myForm”><span><input type=“radio”name=“q12_3”value=“1”>1</span><br><span><input type=“radio”name=“q12_3”value=“2”>2</span></form>
要获取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>
试试这个。这对我有用
$('input[type="radio"][name="name"]:checked').val();
JQuery获取表单中的所有单选按钮和选中的值。
$.each($("input[type='radio']").filter(":checked"), function () {
console.log("Name:" + this.name);
console.log("Value:" + $(this).val());
});
您可以将:checked选择器与收音机选择器一起使用。
$("form:radio:checked").val();