我有两个单选按钮,希望发布所选按钮的值。如何使用jQuery获取值?
我可以得到所有这些:
$("form :radio")
我如何知道选择了哪一个?
我有两个单选按钮,希望发布所选按钮的值。如何使用jQuery获取值?
我可以得到所有这些:
$("form :radio")
我如何知道选择了哪一个?
当前回答
JQuery获取表单中的所有单选按钮和选中的值。
$.each($("input[type='radio']").filter(":checked"), function () {
console.log("Name:" + this.name);
console.log("Value:" + $(this).val());
});
其他回答
在JSF生成的单选按钮(使用<h:selectOneRadio>标记)中,可以执行以下操作:
radiobuttonvalue = jQuery("input[name='form_id\:radiobutton_id']:checked").val();
其中selectOneRadio ID是radiobutton_ID,表单ID是form_ID。
如图所示,请务必使用name而不是id,因为jQuery使用这个属性(名称由JSF自动生成,类似于控件id)。
$("input:radio:checked").val();
JQuery获取表单中的所有单选按钮和选中的值。
$.each($("input[type='radio']").filter(":checked"), function () {
console.log("Name:" + this.name);
console.log("Value:" + $(this).val());
});
您可以调用函数onChange()
<input type="radio" name="radioName" value="1" onchange="radio_changed($(this).val())" /> 1 <br />
<input type="radio" name="radioName" value="2" onchange="radio_changed($(this).val())" /> 2 <br />
<input type="radio" name="radioName" value="3" onchange="radio_changed($(this).val())" /> 3 <br />
<script>
function radio_changed(val){
alert(val);
}
</script>
如果您只想要布尔值,即是否选中,请尝试以下操作:
$("#Myradio").is(":checked")