简体   繁体   English

jQuery foreach复选框

[英]jquery foreach for checkbox

i have jquery for disable buttons if check box checked then button enabled 我有禁用按钮的jQuery查询,如果复选框选中,然后启用按钮

document.getElementById('disabler').onchange = function() {   
                        if ($(disabler).is( ":checked" ) ){
                            $("#signin_submit").prop('disabled', false);
                            $("#signin_submit").css( 'cursor', 'pointer' );
                        }  else { 
                        $("#signin_submit").prop('disabled', true);
                        $("#signin_submit").css( 'cursor', 'not-allowed' );
                        }
                    }
                     });

there is many checkbox but this code work only for first check box only ! 有很多复选框,但是此代码仅适用于第一个复选框!

there is many checkbox but this code work only for first check box only ! 有很多复选框,但是此代码仅适用于第一个复选框!

IDs must be unique. ID必须是唯一的。 . You should class instead. 您应该上课。

Example of using class selector 使用类选择器的示例

$('.disabler').change(function () {
    if ($(this).is(":checked")) {
        $("#signin_submit").prop('disabled', false).css('cursor', 'pointer');
    } else {
        $("#signin_submit").prop('disabled', true).css('cursor', 'not-allowed');
    }
});

Try use only jQuery: 尝试仅使用jQuery:

$('#disabler').change(function() {
    if ($(this).is(":checked")) {
        $("#signin_submit").prop('disabled', false).css('cursor', 'pointer');
    }  else { 
        $("#signin_submit").prop('disabled', true).css('cursor', 'not-allowed');
    }
});

Use this: 用这个:

$(document).on("change", "<selector for disablers>", function() {   
    if (this.checked) {
        $("#signin_submit").prop('disabled', false);
        $("#signin_submit").css('cursor', 'pointer');
    } else { 
        $("#signin_submit").prop('disabled', true);
        $("#signin_submit").css('cursor', 'not-allowed');
    }
});

<selector for disablers> should probably be a class ( .disabler ) rather than id, since you said you have many of them. <selector for disablers>可能应该是类( .disabler )而不是id,因为您说过很多。

Try this: 尝试这个:

         $("#disabler").change(function(){ 
                    if ($("#disabler").is( ":checked" ) ){
                        $("#signin_submit").prop('disabled', false);
                        $("#signin_submit").css( 'cursor', 'pointer' );
                    }  
                  else { 
                        $("#signin_submit").prop('disabled', true);
                        $("#signin_submit").css( 'cursor', 'not-allowed' );
                   }
         });

if you have more than one disabler in you page, firstly you should use class attribute instead of ID , then easily do this: 如果您的页面中有多个disabler ,则首先应使用class属性而不是ID ,然后轻松进行此操作:

$(document).on("change", ".disabler", function(){
    //your stuff
});

ID attribute is used as a unique identifier. ID属性用作unique标识符。

check this working DEMO ; 检查此工作演示 ;

I agree, you are already using jQuery, so use it. 我同意,您已经在使用jQuery,请使用它。

However, and in addition, you are calling the jQuery object far too many times. 但是,此外,您多次调用jQuery对象。 If you are parsing through to get the jQuery object more than once, then you should create local variables. 如果要解析多次以获取jQuery对象,则应创建局部变量。

Second, you might consider using the power of closures for things like this: 其次,您可以考虑对以下内容使用闭包的功能:

function wireDisabler() {
    // closures
    var $cbxDisabler = $('#disabler');
    var $btnSubmit = $("#signin_submit");
    var fOnChange = function() {
        var bChecked = $cbxDisabler.is(':checked');
        $btnSubmit.prop('disabled', !bChecked)
            .css('cursor', (bChecked ? 'pointer' : 'not-allowed'));
    };
    // handle event
    $cbxDisabler.change(fOnChange);
}

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

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