我一直在搜索并尝试许多不同的方法来使此表单在提交时验证单选按钮,但没有成功。
我有一个名为
的表格user_details
我有几个广播组,一个是
overall_experience
还有一个是
promptness
我究竟如何获得简单的 javascript 函数 validateForm 来确保最终用户选择了每个组中的一个单选按钮?
我有这个(请看这里的屏幕截图)
但是无线校验码不对。
有谁知道如何确保最终用户从每个单选按钮组中至少选择了一个选项?
最佳答案
看看这个希望这是你需要的:jsfiddle
<table width="440" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<input type="radio" name="overall_experience" value="1" class="a" />1</td>
<td>
<input type="radio" name="overall_experience" value="2" class="a" />2</td>
<td>
<input type="radio" name="overall_experience" value="3" class="a" />3</td>
<td>
<input type="radio" name="overall_experience" value="4" class="a" />4</td>
<td>
<input name="overall_experience" type="radio" value="5" class="a" />5</td>
</tr>
</table>
<br />
</div>
</div>
<div style="width:100%;clear:both;">
<div class="fullwidth_question"> <span>2. The promptness...they got there when they said they would.</span>
</div>
<div class="fullwidth_answer_indented">
<table width="440" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<input type="radio" name="promptness" value="1" class="b" />1</td>
<td>
<input type="radio" name="promptness" value="2" class="b" />2</td>
<td>
<input type="radio" name="promptness" value="3" class="b" />3</td>
<td>
<input type="radio" name="promptness" value="4" class="b" />4</td>
<td>
<input type="radio" name="promptness" value="5" class="b" />5</td>
</tr>
<input type="button" value="butt" id="butt">
相关的 jquery 是:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script>
<script type = "text/javascript">
$(document).ready(function validater() {
$("#butt").click(function (event) {
if($(".a").is(':checked') && $(".b").is(':checked') ){
alert("ok");
return true;}
else{alert("nok");
return false;}
});
});
</script>
并在表单提交时调用 validater()。
关于javascript - 验证单选按钮被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19035776/