繁体   English   中英

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

[英]adding click event to class with jQuery

我发现找到了几个与此类似的问题答案,我尝试了其中的一些,但是没有运气。 可能我的情况有所不同,当我单击其中的任何一个时,我想禁用其他复选框。 如果第二次单击则禁用。

编辑:

我有两个复选框,即; chk1chk2 如果CHK1检查chk2成为禁用,如果chk2检查CHK1成为禁用

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>

您缺少,可以将jQuery放在

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

您必须将click事件置于文档就绪状态$(function () { });

您可以在这里看到工作示例

暂无
暂无

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

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