单选按钮检查w / jQuery
问题描述:
通过循环遍历页面上的所有单选按钮,并提醒用户其中一个未选中某个特定问题,最好的方法是什么?
What would be the best way to loop through all radio buttons on my page and alert the user if one of them is unchecked for a particular question?
注意:在一页上有多个问题。
Note: there are multiple questions on one page.
<li>
<fieldset>
<h3>What music do you listen to?</h3>
<ul class="answerList">
<li>
<input type="radio" id="choice_22" name="answer8" value="1" class=""/>
<label for="choice_22" class="">Heavy Metal </label>
</li>
<li>
<input type="radio" id="choice_23" name="answer8" value="2" class=""/>
<label for="choice_23" class="">Pop music</label>
</li>
<li>
<input type="radio" id="choice_24" name="answer8" value="3" class=""/>
<label for="choice_24" class="">Oh, a mix of stuff </label>
</li>
</ul>
</fieldset>
</li>
答
if (undefined === $("input[name='answer8']:checked").val()) {
// do something
}