php
  • checkbox
  • 2012-08-05 30 views 1 likes 
    1

    我有一個單選按鈕列表,這些按鈕都被分成組,特別是在這種情況下的學校班級。無效的複選框組

    <label class="control-label" for="optionsCheckboxList">Math</label> 
    <div class="controls"> 
        <label class="checkbox"> 
         <input type='checkbox' name='class[math1]' value='1' id="mathhl"> 
         High Level 
        </label> 
        <label class="checkbox"> 
         <input type='checkbox' name='class[math2]' value='1' id="mathsl"> 
         Standard Level 
        </label> 
        <label class="checkbox"> 
         <input type='checkbox' name='class[math3]' value='1' id="mathst"> 
         Studies 
         </label> 
    </div> 
    <hr> 
    <label class="control-label" for="optionsCheckboxList">Physics</label> 
    <div class="controls"> 
        <label class="checkbox"> 
         <input type="checkbox" name='class[phys1]' value='1' id="physhl"> 
         High Level 
        </label> 
    </div> 
    

    在本例中,數學有多個選項,即高級,標準級和研究。我想知道一種方法,我可以做到這一點,以便當一個人被選中時,其他人被停用,但僅限於同一個班級。因此,例如,如果有人選擇數學高級,另外兩個數學課程被禁用(直到它未被選中),但物理學不受影響。

    請注意,我有很多類,所以我想要一些適用於許多複選框組的東西。

    +3

    收音機盒會更適合這個嗎?聽起來你只有一個選項可以被檢查,這是收音機的用途。 – 2012-08-05 02:18:35

    +0

    您有重複的ID'mathh1' – codingbiz 2012-08-05 02:22:52

    +0

    感謝您的收穫,@codingbiz。另外,我嘗試過使用單選按鈕,但是頁面總是將它作爲一個大組單選按鈕來處理,只讓我爲整個列表選擇一個。 – 2012-08-05 02:25:45

    回答

    1

    ,我建議你在在MDN閱讀筆記:https://developer.mozilla.org/en-US/docs/HTML/Element/Input

    關於type="radio"他們說以下內容:

    必須使用值屬性來定義提交此 項的值。

    與name屬性具有相同值的單選按鈕在 中是相同的「單選按鈕組」。一次只能選擇一個組中的一個單選按鈕 。

    0

    如果使用相同名稱和每組單選按鈕不同值,你會得到你所需要的。

    代碼:

    <?php 
    if(isset($_POST["submit"])){ 
        echo "You selected:<br />\r\n<pre>"; 
        print_r($_POST); 
        echo "</pre>"; 
    } 
    ?> 
    <form action="" method="POST"> 
    <label class="control-label" for="optionsCheckboxList">Math</label> 
    <div class="controls"> 
        <label class="checkbox"> 
         <input type='radio' name='class[math]' value='hl' id="mathhl"> 
         High Level 
        </label> 
        <label class="checkbox"> 
         <input type='radio' name='class[math]' value='sl' id="mathsl"> 
         Standard Level 
        </label> 
        <label class="checkbox"> 
         <input type='radio' name='class[math]' value='st' id="mathst"> 
         Studies 
         </label> 
    </div> 
    <hr> 
    <label class="control-label" for="optionsCheckboxList">Physics</label> 
    <div class="controls"> 
        <label class="checkbox"> 
         <input type="radio" name='class[phys]' value='hl' id="physhl"> 
         High Level 
        </label> 
    </div> 
    <hr> 
    <input type="submit" name="submit" value="Submit" /> 
    </form> 
    

    Live Demo 此設置滿足您的所有描述的條件:

    你說So, for example, if someone chooses Math High Level, the other two math classes are disabled (until it is unchecked), but Physics is unaffected.

    Please note, I have a ton of classes, so I'd like something that works for many groups of check boxes.

    所以,用單選按鈕,class[math]組,只有可以選擇一種類型的課程 一次。數學&物理選擇不會影響彼此或任何其他類。

    但是,在單選按鈕的情況下,您還應該考慮讓用戶選擇No Class的選項,因爲一旦在一個組中選擇了單選按鈕,就不能取消選中(與複選框不同)。所以,你可以把喜歡No Class

    一些選項,如果你想使用複選框,看到了這個問題:How to disable enable a checkbox based on another checkbox?

    這個問題和接受的答案使用jQuery和有類似的需求,如您(組噸),但只處理每個組中的兩個複選框。

    相關問題