jquery复选框仅在第一次加载时触发

时间:2016-04-19 09:09:08

标签: javascript jquery html checkbox

我有一个控制已检查属性的单选按钮,并禁用/启用您已选中的单选按钮上的复选框。

问题是onload我检查了radiobutton 2。然后启用所有复选框。如果我选中了单选按钮1,则必须禁用前2个复选框,并且第3个复选框应保持启用状态,并且必须进行检查。

如果你重复这个场景。检查单选按钮2运行平稳,但如果你回到单选按钮1,最后一个复选框被标记为已检查其属性,但该框没有检查。

基本上需要进行可视化检查以便发送数据。

HTML:

<span><input type="radio" name="attendee" id="guest1" value="1">1</span> <span><input type="radio" name="attendee" id="guest2" value="2" checked="checked">2</span><br>

<span class="wpcf7-form-control wpcf7-checkbox wpcf7-validates-as-required JPCCheck"><span class="wpcf7-list-item first"><input type="checkbox" name="attending2[]" value="14 september - Welcome Dinner">&nbsp;<span class="wpcf7-list-item-label">14 september - Welcome Dinner</span></span><span class="wpcf7-list-item"><input type="checkbox" name="attending2[]" value="15 september Wedding Day">&nbsp;<span class="wpcf7-list-item-label">15 september Wedding Day</span></span><span class="wpcf7-list-item last"><input type="checkbox" name="attending2[]" value="N/A">&nbsp;<span class="wpcf7-list-item-label">N/A</span></span></span>

SCRIPT:

$(function(){
    $("#guest1").click( function(){
       if( $(this).is(":checked") ) {
        $('input[name="attending2[]"]').prop('disabled', true).css("background","#666").attr("checked", false);
        $('.last input[name="attending2[]').prop('disabled', false).css("background","#666").attr("checked", true);
        $('select[name="foodspec2"]').val("N/A").prop('disabled', true).css("background","#666");
        $('select[name="country2"]').val("N/A").prop('disabled', true).css("background","#666");
        $("input[name='salutation2']").val("N/A").prop('readonly', true).css("background","#666");
        $("input[name='name2']").val("N/A").prop('readonly', true).css("background","#666");
        $("textarea[name='address22a']").val("N/A").prop('readonly', true).css("background","#666");
        $("textarea[name='address22b']").val("N/A").prop('readonly', true).css("background","#666");                
        $("input[name='city2']").val("N/A").prop('readonly', true).css("background","#666");
        $("input[name='postal2']").val("000").prop('readonly', true).css("background","#666");
        $("input[name='passcode2']").val("000").prop('readonly', true).css("background","#666");
        return;
       }

    });

    $("#guest2").click( function(){
       if( $(this).is(":checked") ) {
        $('input[name="attending2[]"]').prop('disabled', false).css("background","#dadada").attr("checked", false);
        $('.last input[name="attending2[]').prop('disabled', false).css("background","#666").attr("checked", false);
        $('select[name="foodspec2"]').val(" ").prop('disabled', false).css("background","#dadada");
        $('select[name="country2"]').val(" ").prop('disabled', false).css("background","#dadada");
        $("input[name='salutation2']").val(" ").prop('readonly', false).css("background","#dadada");
        $("input[name='name2']").val(" ").prop('readonly', false).css("background","#dadada");
        $("textarea[name='address22a']").val(" ").prop('readonly', false).css("background","#dadada");
        $("textarea[name='address22b']").val(" ").prop('readonly', false).css("background","#dadada");
        $("input[name='city2']").val(" ").prop('readonly', false).css("background","#dadada");
        $("input[name='postal2']").val(" ").prop('readonly', false).css("background","#dadada");
        $("input[name='passcode2']").val(" ").prop('readonly', false).css("background","#dadada");
    }
    });
});

只要函数按照说法运行,Javascript或jquery解决方案就可以了。

FIDDLE HERE

1 个答案:

答案 0 :(得分:0)

你好,亲爱的按钮上的检查和启用/禁用复选框 更改。如果使用有任何混淆,那么写评论。

<body>
        <span>
            <input type="radio" name="attendee" id="guest1" value="1">
            1</span><span>
            <input type="radio" name="attendee" id="guest2" value="2" checked="checked">
            2</span>
        <br>
        <span class="wpcf7-form-control wpcf7-checkbox wpcf7-validates-as-required JPCCheck"><span class="wpcf7-list-item first">
                <input type="checkbox" name="attending2[]" value="14 september - Welcome Dinner">
                &nbsp;<span class="wpcf7-list-item-label">14 september - Welcome Dinner</span></span><span class="wpcf7-list-item">
                <input type="checkbox" name="attending2[]" value="15 september Wedding Day">
                &nbsp;<span class="wpcf7-list-item-label">15 september Wedding Day</span></span><span class="wpcf7-list-item last">
                <input type="checkbox" name="attending2[]" value="N/A">
                &nbsp;<span class="wpcf7-list-item-label">N/A</span></span></span>
        <script>
            $(function() {
                $(document).on("change", "#guest1, #guest2", function() {
                    var radioVal = $(this).val();
                    var checkLength = $('.wpcf7-form-control .wpcf7-list-item input[type="checkbox"]').length;

                    if (radioVal == 1) {
                        for (var i = 0; i < checkLength; i++) {
                            if (i == (checkLength - 1)) {
                                $('.wpcf7-form-control .wpcf7-list-item input[type="checkbox"]').eq(i).prop("disabled", false);
                                $('.wpcf7-form-control .wpcf7-list-item input[type="checkbox"]').eq(i).prop('checked', true);
                            } else
                                $('.wpcf7-form-control .wpcf7-list-item input[type="checkbox"]').eq(i).prop("disabled", true);
                        }
                    } else {
                        for (var i = 0; i < checkLength; i++) {
                            if (i == (checkLength - 1)) {
                                $('.wpcf7-form-control .wpcf7-list-item input[type="checkbox"]').eq(i).prop("disabled", false);
                                $('.wpcf7-form-control .wpcf7-list-item input[type="checkbox"]').eq(i).prop('checked', false);
                            } else
                                $('.wpcf7-form-control .wpcf7-list-item input[type="checkbox"]').eq(i).prop("disabled", false);
                        }

                    }
                });
            });
        </script>
    </body>