2013-01-04 16 views
6

के आधार पर एक ही कक्षा नाम के साथ सभी चेकबॉक्स को चेक या अनचेक करें मेरे पास एकाधिक divs में निम्न चेकबॉक्स हैं।मूल्य

<div class="div1"> 
    <input type="checkbox" class="chkbox" value="101"> This is 101 
    <input type="checkbox" class="chkbox" value="102"> This is 102 
    <input type="checkbox" class="chkbox" value="103"> This is 103 
</div> 
<div class="div2"> 
    <input type="checkbox" class="chkbox" value="110"> This is 110 
    <input type="checkbox" class="chkbox" value="102"> This is 102 
    <input type="checkbox" class="chkbox" value="101"> This is 101 
</div> 

ऊपर दिखाए गए अनुसार, कुछ चेकबॉक्सों में एकाधिक divs में एक ही मान (उदा। 101) है। अब, जब भी एक चेकबॉक्स चेक किया जाता है, मुझे उसी मूल्य के साथ अन्य चेकबॉक्स जांचना होगा। इसी प्रकार, अनचेक करने के लिए।

$(".chkbox").change(function() { 
    // If checked 
    if (this.checked) 
      // find other checkboxes with same value and check them 
    else 
      // find other checkboxes with same value and uncheck them 
} 

उत्तर

8

तुम कर सकते हो:

$(".chkbox").change(function() { 
    var val = $(this).val(); 
    if($(this).is(":checked")) { 

    $(":checkbox[value='"+val+"']").attr("checked", true); 
    } 
    else { 
     $(":checkbox[value='"+val+"']").attr("checked", false); 
    } 
}); 

डेमो: नीचे वोट के लिए jsFiddle

3
$(".chkbox").change(function() { 
    $(":checkbox[value="+$(this).val()+"]").prop("checked", this.checked); 
} 
0

आप इसे इस तरह कर सकते हैं।

Live Demo

$(".chkbox").change(function() { 
    // If checked 
    if (this.checked) 
      $(":checkbox[value=" + this.value + "]").attr('checked', true); 
    else 
      $(":checkbox[value=" + this.value + "]").attr('checked', false);   // find other checkboxes with same value and uncheck them 
}) 

आप परिवर्तन घटना के समापन paraenthesis ) याद किया।

+0

कारण? – Adil

0
$(".chkbox").change(function() { 
    var value = $(this).attr("value"); 

    $(".chkbox[value='" + value + "']").prop("checked", this.checked); 
}); 
0
// if user has already sent the survey 
    if(user_id_value){ 

     var result = confirm("This user already received a survey. Are you sure you want to resend?"); 

     // do not send the survey again 
     if(!result){    
      $(":checkbox[value='"+user_id+"']").attr("checked", false);   
     }// end if not confirmed 

    }// end if