2017-12-05 4 views
0

Meine Schleife läuft 3 mal.div durchlaufen Schleife und führen deaktivieren und aktivieren Sie die Schaltfläche mit den gleichen Klassen

Innerhalb der Schleife div wird ausgeführt und 3 Mal ausgeführt.

Wenn in Box 1 div Ich Kontrollkästchen aktiviert und wenn es aktiviert ist eine Schaltfläche aktiviert ist, sonst ist die Schaltfläche deaktiviert.

Das funktioniert, aber es läuft auf Schleife, so dass seine 3 Zeit div mit gleichen Klassen speichern.

Wenn ich zuerst div wie Box 1 Checkbox dann nur aktivieren Schaltfläche von Box 1 div andere div nicht betroffen.

Wenn ich Kontrollkästchen 3 Kontrollkästchen nur dann deaktivieren Sie diese Schaltfläche nicht andere.

Wie ist das möglich?

Mein Code

$(document).ready(function(){ 
 

 
    $('body').on('click' , '.currentcheck' , function() { 
 
     $(this).toggleClass("checked"); 
 

 
     if ($(".currentcheck.checked").length>0) { 
 
     $('.btn_disable').prop('disabled', false); 
 
     } else { 
 
     $('.btn_disable').prop('disabled', true); 
 
     } 
 
    }); 
 
    
 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Test Your Self</title> 
 
    <meta name="viewport" content="width=device-width initial-state=1"/> 
 
</head> 
 
<body>  
 
\t <form> 
 
\t <div class="box"> 
 
\t \t <div class="element_checkbox"> 
 
\t \t  <p>box 1</p> 
 
\t \t \t <input type="checkbox" name="chec1" class="currentcheck"> 
 
\t \t \t <input type="checkbox" name="chec1" class="currentcheck"> 
 
\t \t \t <input type="checkbox" name="chec1" class="currentcheck"> 
 
\t \t \t <input type="checkbox" name="chec1" class="currentcheck"> 
 
      <button class="btn_disable" disabled>next</button> 
 
\t \t </div> 
 
\t \t <div class="element_checkbox"> 
 
\t \t  <p>box 2</p> 
 
\t \t \t <input type="checkbox" name="chec1" class="currentcheck"> 
 
\t \t \t <input type="checkbox" name="chec1" class="currentcheck"> 
 
\t \t \t <input type="checkbox" name="chec1" class="currentcheck"> 
 
\t \t \t <input type="checkbox" name="chec1" class="currentcheck"> 
 
\t \t \t <button class="btn_disable" disabled>next</button> 
 
\t \t </div> 
 
\t \t <div class="element_checkbox"> 
 
\t \t  <p>box 3</p> 
 
\t \t \t <input type="checkbox" name="chec1" class="currentcheck"> 
 
\t \t \t <input type="checkbox" name="chec1" class="currentcheck"> 
 
\t \t \t <input type="checkbox" name="chec1" class="currentcheck"> 
 
\t \t \t <input type="checkbox" name="chec1" class="currentcheck"> 
 
\t \t \t <button class="btn_disable" disabled>next</button> 
 
\t \t </div> 
 
\t </div> 
 
\t </form> 
 

 
</body> 
 
</html>

+0

So Gruppen Sie sind neu zu erfinden Radio-Button? – epascarello

Antwort

0

$(document).ready(function() { 
 

 
    $('body').on('click', '.currentcheck', function() { 
 
    var $this = $(this); 
 
    //get the parent that encapsulates the checkboxes related to the one that was clicked 
 
    var $parent = $this.closest('.element_checkbox'); 
 
    
 
    $this.toggleClass("checked"); 
 

 
    //only look at the checkboxes in the parent 
 
    //only change the btn in the parent 
 
    if ($parent.find(".currentcheck.checked").length > 0) { 
 
     $parent.find('.btn_disable').prop('disabled', false); 
 
    } else { 
 
     $parent.find('.btn_disable').prop('disabled', true); 
 
    } 
 
    }); 
 

 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title>Test Your Self</title> 
 
    <meta name="viewport" content="width=device-width initial-state=1" /> 
 
</head> 
 

 
<body> 
 
    <form> 
 
    <div class="box"> 
 
     <div class="element_checkbox"> 
 
     <p>box 1</p> 
 
     <input type="checkbox" name="chec1" class="currentcheck"> 
 
     <input type="checkbox" name="chec1" class="currentcheck"> 
 
     <input type="checkbox" name="chec1" class="currentcheck"> 
 
     <input type="checkbox" name="chec1" class="currentcheck"> 
 
     <button class="btn_disable" disabled>next</button> 
 
     </div> 
 
     <div class="element_checkbox"> 
 
     <p>box 2</p> 
 
     <input type="checkbox" name="chec1" class="currentcheck"> 
 
     <input type="checkbox" name="chec1" class="currentcheck"> 
 
     <input type="checkbox" name="chec1" class="currentcheck"> 
 
     <input type="checkbox" name="chec1" class="currentcheck"> 
 
     <button class="btn_disable" disabled>next</button> 
 
     </div> 
 
     <div class="element_checkbox"> 
 
     <p>box 3</p> 
 
     <input type="checkbox" name="chec1" class="currentcheck"> 
 
     <input type="checkbox" name="chec1" class="currentcheck"> 
 
     <input type="checkbox" name="chec1" class="currentcheck"> 
 
     <input type="checkbox" name="chec1" class="currentcheck"> 
 
     <button class="btn_disable" disabled>next</button> 
 
     </div> 
 
    </div> 
 
    </form> 
 

 
</body> 
 

 
</html>

Verwandte Themen