我有一个包含10个字段集的表单。
我希望能够在提交表单时检查每个字段集是否已选中单选按钮。
我已经搜索了类似的帖子并尝试了大部分/全部答案但没有成功,所以想知道是否有人可以就我出错的地方提供一些建议?
html的一个例子:
func readDataFromFile(){
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let directory = paths[0]
let filePath = directory.stringByAppendingPathComponent(self.fileName)
print(filePath) // prints the same path
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(filePath){
data = fileManager.contentsAtPath(filePath)
}else{
print("*****BAD*****") // always prints this which means that file wasn`t created
}
}
这是我使用jQuery的地方:
<div class="questions">
<fieldset>
<h2>If you could have any superpower, what would it be? </h2>
<div class="row question9">
<label for="Q9A1" class="Q9 A1">
<input type="radio" class="styled" id="Q9A1" value="Q9A1" name="Q9">
Flight </label>
<label for="Q9A2" class="Q9 A2">
<input type="radio" class="styled" id="Q9A2" value="Q9A2" name="Q9">
Super speed </label>
<label for="Q9A3" class="Q9 A3">
<input type="radio" class="styled" id="Q9A3" value="Q9A3" name="Q9">
Xray vision </label>
<label for="Q9A4" class="Q9 A4">
<input type="radio" class="styled" id="Q9A4" value="Q9A4" name="Q9">
Super strength </label>
<label for="Q9A5" class="Q9 A5">
<input type="radio" class="styled" id="Q9A5" value="Q9A5" name="Q9">
See into the future </label>
</div>
</fieldset>
<div>
<div class="questions">
<fieldset>
<h2>If you could have any superpower, what would it be?</h2>
<div class="row question10">
<label for="Q10A1" class="Q10 A1">
<input type="radio" class="styled" id="Q10A1" value="Q10A1" name="Q10">
Flight </label>
<label for="Q10A2" class="Q10 A2">
<input type="radio" class="styled" id="Q10A2" value="Q10A2" name="Q10">
Super speed </label>
<label for="Q10A3" class="Q10 A3">
<input type="radio" class="styled" id="Q10A3" value="Q10A3" name="Q10">
Xray vision </label>
<label for="Q10A4" class="Q10 A4">
<input type="radio" class="styled" id="Q10A4" value="Q10A4" name="Q10">
Super strength </label>
<label for="Q10A5" class="Q10 A5">
<input type="radio" class="styled" id="Q10A5" value="Q10A5" name="Q10">
See into the future </label>
</div>
</fieldset>
</div>
答案 0 :(得分:1)
我认为你可以这样做:
$('#submit').on('click', function() {
var flag = true;
$('.questions').each(function() {
if ($(this).find(':checkbox:checked').length == 0) {
flag = false;
return false;
}
});
if (flag)
alert("submitted");
else
alert("Not submitted");
});
尝试一下,让我知道它是否有帮助!
修改
如果您使用单选按钮,因为每个问题只能检查一个,另一种方法是:
$('#submit').on('click', function() {
var totalQuestions = $('.questions').length;
var totalAnswers = $('.questions').find(':radio:checked').length;
if (totalQuestions == totalAnswers)
alert("submitted");
else
alert("Not submitted");
});
答案 1 :(得分:0)
您也可以尝试以下方法:
$(document).ready(function() {
$("#submit").click(function(){
var flag;
$('.questions').each(function(){
flag = false;
var groupName = $(this).find("input[type='radio']:first").attr('name');
var answer = $("input[name="+ groupName +"]:checked").val();
if(typeof answer != "undefined" && answer != "undefined"){
flag = true;
}
else
{
flag = false;
return false;
}
});
if(flag == false){
alert("Please select all the answers");
}
else{
alert("all the answers are selected");
}
});
});