2015-12-21 157 views
0

我在創建一些JavaScript時遇到了一些問題。當所有選中的項目都是未選中時,它需要從選中的全部框中刪除選中的項目,同樣當全選項被選中時,我不想通過其他項目只選中全部選中項。檢查所有複選框

check all, item 1, item 2 

刪除項目1,並檢查所有未檢查,所有得到通過的項目。如果勾選全部勾選,則選中項目1和項目2,但不會拉動。

// all "check all" checkboxes will have the class "checkall" 
 
// and the id to match the location, e.g. wales, etc 
 

 
$(".checkall").on('change', function() { 
 

 
    // all normal checkboxes will have a class "chk_xxxxxx" 
 
    // where "xxxx" is the name of the location, e.g. "chk_wales" 
 

 
    // get the class name from the id of the "check all" box 
 
    var checkboxesClass = '.chk_' + $(this).attr("id"); 
 

 
    // now get all boxes to check 
 
    var boxesToCheck = $(checkboxesClass); 
 

 
    // check all the boxes 
 
    boxesToCheck.prop('checked', this.checked); 
 
}); 
 

 

 
// display what the user has chosen 
 
$("input[type='checkbox']").change(function() { 
 

 
    $("#checked").empty(); 
 

 
    $("input[type='checkbox']:checked").each(function() { 
 

 
    // see if it is a "check all" box 
 
    if ($(this).attr("class") === "checkall") { 
 
     // Display the id of the "check all" box 
 
     $("#checked").html($("#checked").html() + '<h3>' + $(this).attr("id").toUpperCase() + "</h3>"); 
 
    } else { 
 
     // display the label text for all normal checkboxes 
 
     $("#checked").html($("#checked").html() + $(this).next().text() + "<br>"); 
 
    } 
 

 
    }); 
 
}); 
 

 

 
// With some more work you could make the Check All headings show when only one or two checkboxes were checked. It depends how you need it to work.
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 

 
    <meta charset="utf-8"> 
 
    <title>Test</title> 
 
</head> 
 

 
<body> 
 

 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
    <input type="checkbox" id="wales" class="checkall" value="1"> 
 
    <label for="wales">Check All</label> 
 
    <input type="checkbox" id="checkItem1" value="2" class="chk_wales"> 
 
    <label for="checkItem1">Item 1</label> 
 
    <input type="checkbox" id="checkItem2" value="2" class="chk_wales"> 
 
    <label for="checkItem2">Item 2</label> 
 
    <input type="checkbox" id="checkItem3" value="2" class="chk_wales"> 
 
    <label for="checkItem3">Item 3</label> 
 

 
    <hr /> 
 

 
    <input type="checkbox" id="west" class="checkall" value="3"> 
 
    <label for="west">Check All</label> 
 
    <input type="checkbox" id="checkItem4" value="4" class="chk_west"> 
 
    <label for="checkItem4">Item 1</label> 
 
    <input type="checkbox" id="checkItem5" value="4" class="chk_west"> 
 
    <label for="checkItem5">Item 2</label> 
 
    <input type="checkbox" id="checkItem6" value="4" class="chk_west"> 
 
    <label for="checkItem6">Item 3</label> 
 

 
    <hr /> 
 

 
    <input type="checkbox" id="east" class="checkall" value="5"> 
 
    <label for="east">Check All</label> 
 
    <input type="checkbox" id="checkItem7" value="6" class="chk_east"> 
 
    <label for="checkItem7">Item 1</label> 
 
    <input type="checkbox" id="checkItem8" value="6" class="chk_east"> 
 
    <label for="checkItem8">Item 2</label> 
 
    <input type="checkbox" id="checkItem9" value="6" class="chk_east"> 
 
    <label for="checkItem9">Item 3</label> 
 

 
    <p>You have selected:</p> 
 

 
    <div id="checked"> 
 

 

 
    </div> 
 

 

 

 
</body> 
 

 
</html>

+0

你能解釋清楚嗎? – void

回答

0

如果我不是錯了你想說這個代碼應工作。我已經完成了第一組複選框。您可以爲未來兩年做:

$("#check1 input[type=checkbox]").on("change", function() { 
 
    if($("#check1 input[type=checkbox]:checked").length == $("#check1 input[type=checkbox]").length) { 
 
     $("#wales").prop("checked",true); 
 
    } 
 
    else { 
 
     $("#wales").prop("checked",false); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="checkbox" id="wales" class="checkall" value="1"> 
 
<label for="wales">Check All</label> 
 
<section id="check1"> 
 
    <input type="checkbox" id="checkItem1" value="2" class="chk_wales"> 
 
    <label for="checkItem1">Item 1</label> 
 
    <input type="checkbox" id="checkItem2" value="2" class="chk_wales"> 
 
    <label for="checkItem2">Item 2</label> 
 
    <input type="checkbox" id="checkItem3" value="2" class="chk_wales"> 
 
    <label for="checkItem3">Item 3</label> 
 
</section>

UPDATE

這將滿足您的要求。刪除您$( 「輸入[類型= '複選框']」)。變化(函數()代碼,並使用更新後的一個

$("#check1 input[type=checkbox]").on("change", function() 
 
    { 
 
     $("#checked").empty(); 
 
     if($("#check1 input[type=checkbox]:checked").length == $("#check1 input[type=checkbox]").length) 
 
     { 
 

 
      $("#wales").prop("checked", true); 
 

 
      // Display the id of the "check all" box 
 
      $("#checked").html($("#checked").html() + "<h3>Wales</h3>"); 
 

 
     } 
 
     else 
 
     { 
 
      $("#wales").prop("checked", false); 
 
      $("#check1 input[type=checkbox]:checked").each(function() 
 
      { 
 
       $("#checked").html($("#checked").html() + $(this).next().text() + "<br>"); 
 
      }); 
 
     } 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="checkbox" id="wales" class="checkall" value="1"> 
 
<label for="wales">Check All</label> 
 
<section id="check1"> 
 
    <input type="checkbox" id="checkItem1" value="2" class="chk_wales"> 
 
    <label for="checkItem1">Item 1</label> 
 
    <input type="checkbox" id="checkItem2" value="2" class="chk_wales"> 
 
    <label for="checkItem2">Item 2</label> 
 
    <input type="checkbox" id="checkItem3" value="2" class="chk_wales"> 
 
    <label for="checkItem3">Item 3</label> 
 
</section> 
 
<hr/> 
 
<div id="checked"> 
 

 

 
</div>

另一個更新

$(".checkall").on('change', function() 
 
    { 
 

 
     // all normal checkboxes will have a class "chk_xxxxxx" 
 
     // where "xxxx" is the name of the location, e.g. "chk_wales" 
 

 
     // get the class name from the id of the "check all" box 
 
     var checkboxesClass = '.chk_' + $(this).attr("id"); 
 

 
     // now get all boxes to check 
 
     var boxesToCheck = $(checkboxesClass); 
 

 
     // check all the boxes 
 
     boxesToCheck.prop('checked', this.checked); 
 
    }); 
 

 
$("#check1 input[type=checkbox], #wales").on("change", function() 
 
    { 
 
     checkBoxes(); 
 
    }); 
 

 
    function checkBoxes() 
 
    { 
 

 
     $("#checked").empty(); 
 
     if($("#check1 input[type=checkbox]:checked").length == $("#check1 input[type=checkbox]").length) 
 
     { 
 

 
      $("#wales").prop("checked", true); 
 

 
      // Display the id of the "check all" box 
 
      $("#checked").html($("#checked").html() + "<h3>Wales</h3>"); 
 

 
     } 
 
     else 
 
     { 
 
      $("#wales").prop("checked", false); 
 
      $("#check1 input[type=checkbox]:checked").each(function() 
 
      { 
 
       $("#checked").html($("#checked").html() + $(this).next().text() + "<br>"); 
 
      }); 
 
     } 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<input type="checkbox" id="wales" class="checkall" value="1"> 
 
<label for="wales">Check All</label> 
 
<section id="check1"> 
 
    <input type="checkbox" id="checkItem1" value="2" class="chk_wales"> 
 
    <label for="checkItem1">Item 1</label> 
 
    <input type="checkbox" id="checkItem2" value="2" class="chk_wales"> 
 
    <label for="checkItem2">Item 2</label> 
 
    <input type="checkbox" id="checkItem3" value="2" class="chk_wales"> 
 
    <label for="checkItem3">Item 3</label> 
 
</section> 
 
<hr/> 
 
<div id="checked"> 
 

 

 
</div>

+0

確定它的工作,但只有當你刪除2項目,如果你刪除項目3第一威爾士doent刪除 –

+0

啊這麼接近,當你刪除一個項目時,他們都檢查它不刪除檢查所有.. http:// jsbin。 com/xiqemiqadu /編輯?html,js,輸出 –

+0

現在它不會在檢查時顯示所有項目,但是再次運行其他項目 –

0

不知道你的意思是「渡過難關」,但取消辦理登機手續都可以通過實現:

if (!$currentCheckbox.hasClass('checkall')) {  
    var $previousCheckAll = $currentCheckbox.prevAll('.checkall');   
    if ($previousCheckAll.length>0) 
    { 
     $previousCheckAll.first().prop('checked',false);  
    }   
} 

只需插入上面的代碼行

$("#checked").empty(); 
您JSbin的

之下。