我们再来一次,搜索并尝试自己,但没有一个在线示例与我的情况不一样。 我有2个基于无线电的问题,其他22个有复选框。 我的问题是如何检查每个问题上是否至少选中了1个复选框/收音机,如果不是,则显示javascript警报 EG:“你必须在问题3上选择至少一个答案”。
注意:警报必须包含问题编号。检查必须在表单提交时完成(如果失败则返回false,否则为true,将被处理。)
以下示例:
<tr><th>- Question 1</th></tr>
<tr><td><input name="01" type="radio" id="01" value="F">
F</td></tr>
<tr><td><input name="01" type="radio" id="01" value="B">
B</td></tr>
<tr><th>- Question 2</th></tr>
<tr><td><input name="02" type="radio" id="02" value="9">
9</td></tr>
<tr><td><input name="02" type="radio" id="02" value="10">
10</td></tr>
<tr><td><input name="02" type="radio" id="02" value="11">
11</td></tr>
<tr><td><input name="02" type="radio" id="02" value="12">
12</td></tr>
<tr><th> Question 3</tr></th>
<tr><td><input name="c1" type="checkbox" id="c1" value="a">
This is checkbox1</td></tr>
<tr><td><input name="c1" type="checkbox" id="c1" value="b">
This is checkbox2</td></tr>
答案 0 :(得分:2)
编辑 - 看到标记后编辑
$('form').submit(function() {
var ok = true;
var that = this;
//get the <tr> inside the function, i assume thatyou have one question per <tr>
$('tr th', this).each(function(idx, el) {
var id = this.id;
if (id) {
if ($('input:checked[name=' + id + ']', that).length === 0) {
alert("You must check at least one option for question " + $(this).text());
ok = false;
}
}
});
if (ok === false) {
return false;
}
});
这是我使用的标记(在此http://jsfiddle.net/R7uwa/2/)
<form id="chestionar" name="chestionar" method="post" action="prelucrare_chest.php">
<table style="width:100%; border-spacing:0;">
<tbody><tr><th>- Selectati sex-ul:</th></tr>
<tr><td><input type="radio" value="F" id="01" name="01">
Fata</td></tr>
<tr><td><input type="radio" value="B" id="01" name="01">
Baiat</td></tr>
<tr><th>- Selectati clasa:</th></tr>
<tr><td><input type="radio" value="9" id="02" name="02">
9</td></tr>
<tr><td><input type="radio" value="10" id="02" name="02">
10</td></tr>
<tr><td><input type="radio" value="11" id="02" name="02">
11</td></tr>
<tr><td><input type="radio" value="12" id="02" name="02">
12</td></tr>
<tr><th id="c1">1. Care sunt motivele pentru care absentezi tu si colegii tai?</th></tr>
<tr><td><input type="checkbox" value="a" id="c1" name="c1">
Nu ne place ora</td></tr>
<tr><td><input type="checkbox" value="b" id="c1" name="c1">
Nu ne intereseaza scoala</td></tr>
<tr><td><input type="checkbox" value="c" id="c1" name="c1">
Nu ne place profesorul</td></tr>
<tr><td><input type="checkbox" value="d" id="c1" name="c1">
Daca absentam nu ni se întâmpla mare lucru</td></tr>
<tr><td><input type="checkbox" value="e" id="c1" name="c1">
Ne plictisim la scoala</td></tr>
<tr><td><input type="checkbox" value="f" id="c1" name="c1">
Facem usor rost de scutiri pentru motivarea absentelor</td></tr>
<tr><td><input type="checkbox" value="g" id="c1" name="c1">
Evitam în felul acesta luarea unei note mici</td></tr>
<tr><td><input type="checkbox" value="h" id="c1" name="c1">
Nu dorim sa intram în conflict cu ceilalti colegi</td></tr>
<tr><td><input type="checkbox" value="i" id="c1" name="c1">
Programul este prea încarcat</td></tr>
<tr><th id='c2'>2. Ce se întâmpla când un elev absenteaza nemotivat mai mult timp?</th></tr>
<tr><td><input type="checkbox" value="a" id="c2" name="c2">
I se scade nota la purtare</td></tr>
<tr><td><input type="checkbox" value="b" id="c2" name="c2">
Sunt chemati parintii lui la scoala</td></tr>
<tr><td><input type="checkbox" value="c" id="c2" name="c2">
Este mustrat de catre diriginte</td></tr>
<tr><td><input type="checkbox" value="d" id="c2" name="c2">
Este evaluat la ora urmatoare si este depunctat la nota</td></tr>
<tr><td><input type="checkbox" value="e" id="c2" name="c2">
Este exmatriculat</td></tr>
<tr><td><input type="checkbox" value="f" id="c2" name="c2">
Nu se întâmpla mare lucru</td></tr>
<tr><th id='c3'>3. Cât de des absentezi nemotivat?</th></tr>
<tr><td><input type="checkbox" value="a" id="3" name="3">
Deloc</td></tr>
<tr><td><input type="checkbox" value="b" id="c3" name="c3">
1-3 ore pe saptamâna</td></tr>
<tr><td><input type="checkbox" value="c" id="c3" name="c3">
3-6 ore pe saptamâna</td></tr>
<tr><td><input type="checkbox" value="d" id="c3" name="c3">
6-9 ore pe saptamâna</td></tr>
<tr><td><input type="checkbox" value="e" id="c3" name="c3">
9-12 ore pe saptamânat</td></tr>
<tr><td><input type="checkbox" value="f" id="c3" name="c3">
12-15 ore pe saptamâna</td></tr>
<tr><td><input type="checkbox" value="g" id="c3" name="c3">
Peste 15 ore pe saptamâna</td></tr>
</tbody></table>
<div class="form_settings"><p style="padding-top: 15px"><span> </span><input type="submit" value="Trimite" name="contact_submitted" class="submit"></p></div></form>
答案 1 :(得分:0)
输入name =“c1 []”type =“checkbox”id =“c1”value =“a”
输入name =“c1 []”type =“checkbox”id =“c1”value =“b”
$(document).ready(function() {
$('form').submit(function(){
var checkbox_value = [];
$('#c1:checked').each(function(i){
checkbox_value[i] = $(this).val();
});
if(checkbox_value==""){
alert("Please Check Minimum One");
return false;
}
}
`