简体   繁体   English

使用jQuery将click事件添加到类中

[英]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 . chk1chk2 if chk1 is checked chk2 become disable, if chk2 is checked chk1 become disable 如果CHK1检查chk2成为禁用,如果chk2检查CHK1成为禁用

HTML 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 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 您缺少,可以将jQuery放在

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

You have to put your click event in document ready state $(function () { }); 您必须将click事件置于文档就绪状态$(function () { });

You can see here working Example 您可以在这里看到工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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