2017-01-07 72 views
0

我有一個選擇/取消選擇所有複選框腳本,如果我檢查特定的複選框,所有複選框將被選中。但我希望它能夠在部分內工作,因此只有部分1中的複選框會受到該部分中所有複選框的影響。即使身份證會工作。只是以任何方式隻影響同一部分標記中的複選框。如何讓JavaScript隻影響某個div內的元素?

當前腳本:[?像這樣]

$("#DE-all-checkboxes").change(function() { 
    $("input:checkbox").prop('checked', $(this).prop("checked")); 
}); 
+0

顯示你的HTML代碼 – ricky

+0

(https://jsfiddle.net/9ukgw44n/1/) – Teemu

回答

0
<html> 
    <head> 
     <script 
       src="https://code.jquery.com/jquery-3.1.1.min.js" 
       integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" 
       crossorigin="anonymous"></script> 

    </head> 
    <body> 
    Check Uncheck All In Section 1 <input id="DE-all-checkboxes" type="checkbox"/>   
     <div id="section1"> 
      Section 1: 
      <input type="checkbox"/> 
      <input type="checkbox"/> 
      <input type="checkbox"/> 
      <input type="checkbox"/> 
      <input type="checkbox"/> 
     </div> 
     <div id="section2">   
      Section 2: 
      <input type="checkbox"/> 
      <input type="checkbox"/> 
      <input type="checkbox"/> 
      <input type="checkbox"/> 
      <input type="checkbox"/> 
     </div> 
     <script> 
      $("#DE-all-checkboxes").change(function() {    
       $("#section1 > input:checkbox").prop('checked', $(this).prop("checked")); 
      }); 
     </script> 
    </body> 
</html> 
相關問題