简体   繁体   中英

adding click event to class with jQuery

I found found several question's answers similar to this, I tried some of them, but no luck. May my scenario is different, I want to make other checkbox disable when anyone of them is clicked. if one is clicked second go disabled.

Edit:

I have two checkboxes ie; chk1 and chk2 . if chk1 is checked chk2 become disable, if chk2 is checked chk1 become disable

HTML

<tr class="cart-item-row">
<td class="remove-from-cart">
<label class="td-title">Remove:</label>
     <input type="checkbox" name="removefromcart" value="4392" tabindex="15" class="class2">
</td>
<td class="add-to-cart">
<label class="td-title">Buy Now:</label>
    <input type="checkbox" name="addtocart" value="4392" tabindex="16" class="class2">
</td>                                                                                                                      

 </tr>

JS

<script>
    $(function () { // I am also adding class to checkbox through JS
        $('.cart-item-row > td > input[type="checkbox"]').addClass("class2");
    });
</script>
<script>
    //$('input[class^="class"]').click(function () {
 //$('.class2').unbind().click(function () {
    $('.class2').click(function () {
        var $this = $(this);

        if ($this.is(".class2")) {
            if ($this.is(":checked")) {
                $(".class2").not($this).prop({ disabled: true, checked: false });

            } else {
                $(".class2").prop("disabled", false);
            }
        }
    });
</script>

You have missing , You can place your jquery within

$(document).ready(function(){ }

You have to put your click event in document ready state $(function () { });

You can see here working Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM