简体   繁体   English

在jQuery中,我的功能无法正常工作

[英]In jQuery my function is not work

I trying to make two conditional statement with jQuery like this. 我试图用jQuery这样做两个条件语句。

$('#user_name, #user_email, #user_password').keyup(function () {
    if ($('#output-name').is('.field-output-comple') &&
            $('#output-email').is('.field-output-comple') &&
            $('#output-password').is('.field-output-comple') &&
            $('#confirm-terms-of-use').is(':checked')
    ) {
        $('#register-button').removeAttr('disabled');
    } else {
        $('#register-button').attr('disabled', 'disabled');
    }
});

$('#confirm-terms-of-use').change(function() {
    if ($('#output-name').is('.field-output-comple') &&
            $('#output-email').is('.field-output-comple') &&
            $('#output-password').is('.field-output-comple') &&
            $('#confirm-terms-of-use').is(':checked')
    ) {
        $('#register-button').removeAttr('disabled');
    } else {
        $('#register-button').attr('disabled', 'disabled');
    }
});

But you know this code is duplicated. 但是您知道此代码是重复的。

if ($('#output-name').is('.field-output-comple') &&
    $('#output-email').is('.field-output-comple') &&
    $('#output-password').is('.field-output-comple') &&
    $('#confirm-terms-of-use').is(':checked')
) { 
    $('#register-button').removeAttr('disabled');
} else {
    $('#register-button').attr('disabled', 'disabled');
}

So I make this code with function like this. 所以我用这样的功能制作这段代码。

$('#user_name, #user_email, #user_password').keyup(isValidPassword())
$('#confirm-terms-of-use').change(isValidPassword())

function isValidPassword() {
    if ($('#output-name').is('.field-output-comple') &&
            $('#output-email').is('.field-output-comple') &&
            $('#output-password').is('.field-output-comple') &&
            $('#confirm-terms-of-use').is(':checked')
    ) {
        $('#register-button').removeAttr('disabled');
    } else {
        $('#register-button').attr('disabled', 'disabled');
    }
}

But it does not worked well. 但是,效果不佳。 No error but isValidPassword dose not output anything maybe. 没有错误,但是isValidPassword可能不输出任何内容。 Where did I make mistake? 我在哪里弄错了? Thank you for reading. 感谢您的阅读。

You need to pass the function reference to the event handlers, you are calling the function isValidPassword and is passing the value returned by it undefined as the event handler. 您需要将函数引用传递给事件处理程序,您要调用函数isValidPassword并将其undefined的值传递为事件处理程序。

$('#user_name, #user_email, #user_password').keyup(isValidPassword)
$('#confirm-terms-of-use').change(isValidPassword)

This isn't doing what you think: 这没有按照您的想法做:

.keyup(isValidPassword())

This doesn't pass the isValidPassword function itself to keyup , it executes the function and passes the result of that function. 这不会将isValidPassword函数本身传递给keyup ,它会执行该函数并传递该函数的结果 And that function doesn't return anything, so its result is undefined . 而且该函数不返回任何内容,因此其结果是undefined

Don't execute the function, just pass the function itself as a variable: 不要执行函数,只需将函数本身作为变量传递:

.keyup(isValidPassword)

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

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