2017-01-07 73 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> 
相关问题