2012-03-27 76 views
1

在這裏,我們再次去搜索並嘗試自己,但沒有一個在線示例與我的情況不一樣。 我有2個基於電臺的問題,其他22個有複選框。 我的問題是如何檢查每個問題上是否至少選擇了1個複選框/收音機,並在不是的情況下顯示javascript警報 EG:「您必須在問題3中至少選擇一個答案」。檢查是否至少選擇了一個複選框PER問題

注意:警報必須包含問題編號。該支票必須在形式進行提交(如果它失敗,它會返回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

你有相同ID的多個元素。這是不允許的。 – Quentin 2012-03-27 15:36:35

+0

@Quentin從我的理解他們必須是這樣的分組他們。 – virusbogdan 2012-03-27 15:37:53

+0

@virusbogdan - 不,它們必須具有相同的*名稱*。他們的ID仍然需要獨特。 – Quentin 2012-03-27 15:39:16

回答

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>&nbsp;</span><input type="submit" value="Trimite" name="contact_submitted" class="submit"></p></div></form> 
+0

檢查/取消選中複選框時,我只會收到隨機提醒。 – virusbogdan 2012-03-27 15:49:39

+0

你能發佈你的表單的完整html嗎? – 2012-03-27 15:50:23

+0

[鏈接](http://laurian.ro/proiect/q2/chestionar.php)哦,前2個電臺選項不是問題。我不希望他們進入'idx'計數。 – virusbogdan 2012-03-27 15:51:52

0

我輸入名稱=「c1 []」type =「checkbox」id =「c1」value =「a」

input 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; } }

`

相關問題