我已经看过有关此事的问题,但我仍然无法做到。我如何获得这些单选按钮的价值?我不应该这样做吗? HTML:
<label for 'planet'>Planet:</label> <br>
<input id ='mercury' type ='radio' name='planets' value='mercury'>Mercury<br>
<input id ='venus'type ='radio' name='planets' value='venus'>Venus<br>
<input id ='earth'type ='radio' name='planets' value='earth'>Earth<br>
<input id ='mars'type ='radio' name='planets' value='mars'>Mars<br>
<input id ='jupiter'type ='radio' name='planets' value='jupiter'>Jupiter<br>
<input id ='saturn'type ='radio' name='planets' value='saturn'>Saturn<br>
<input id ='uranus'type ='radio' name='planets' value='uranus'>Uranus<br>
<input id ='neptune'type ='radio' name='planets' value='neptune'>Neptune<br><br>
<button id='submit'>SUBMIT</button>
的javascript:
$('#submit').on('click',function(){
$("input:radio").attr("checked", false);
var age = document.getElementById('age');
age.value=' ';
var checked = document.getElementsByName('planets').checked.value;
alert(checked);
});
答案 0 :(得分:0)
$("input[name='planets']:checked").val()
<强> JSFIDDLE DEMO 强>
编辑:
我在你的提交处理程序中看到这一行,这将取消选中所有单选按钮,所以不要这样做..
$("input:radio").attr("checked", false);
答案 1 :(得分:0)
你可以得到这样的价值 -
checked = $('input:radio[name="planets"]:checked').val();