我有一个类似于下面的表格:
<form id="myForm">
Question 1:<br>
<input type="radio" name="question1" value="Yes"> Yes
<input type="radio" name="question1" value="No"> No
<br>
Question 2:<br>
<input type="radio" name="question2" value="Yes"> Yes
<input type="radio" name="question2" value="No"> No
<br>
Question 3:<br>
<input type="radio" name="question3" value="Yes"> Yes
<input type="radio" name="question3" value="No"> No
<br>
<input type="submit" value="Submit" name="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
我想使用 jQuery 从所有问题中获取所有选定的单选按钮值,如果所有值都等于“是”,它将提醒成功,否则将提醒失败。
这是我写的jQuery:
$(document).ready(function(){
$("#myForm input[type=radio]:checked").each(function() {
var value = $(this).val();
});
});
Run Code Online (Sandbox Code Playgroud)
您可以检查是否在无线电检查中没有得到任何结果,否则结果是失败或成功。
result = "success";
$("#myForm input[type=radio]:checked").each(function() {
if(this.value == "No" && this.checked == true)
{
result = "fail";
return false;
}
});
alert(result);
Run Code Online (Sandbox Code Playgroud)