简体   繁体   English

'keypress' jQuery 事件是否在输入值更新之前触发?

[英]Does 'keypress' jQuery event fire before an inputs value is updated?

I have a keypress event listener on an input field to confirm a password.我在输入字段上有一个按键事件侦听器来确认密码。 I want the button on the page to be disabled until the password and confirm-password fields have matching values.我希望页面上的button被禁用,直到passwordconfirm-password字段具有匹配的值。 I am using the .keypress() jQuery function, but it seems to always be one character behind what I expect it to be?我正在使用.keypress() jQuery 函数,但它似乎总是落后于我期望的一个字符?

Here is the code这是代码

$('#confirm').keypress(function() {
    console.log('#confirm').val();
    if($('#password').val() == $('#confirm').val()) {
        $('button').removeProp('disabled');
        console.log("yes");                
    } else {
        console.log("no");
    }
});

But when I inspect element and look at the console window on my page, the first time the event is fired it prints the form value as blank.但是当我检查元素并查看页面上的控制台窗口时,第一次触发事件时,它会将表单值打印为空白。 Then when I enter a second character, it prints only the first, and when I type a third character it prints the first two, etc.然后当我输入第二个字符时,它只打印第一个字符,当我输入第三个字符时,它打印前两个字符,依此类推。

For example, if I put asd into the password field and begin typing the same into the confirm field the output will look like this:例如,如果我将asd放入password字段并开始在confirm字段中输入相同的内容,则输出将如下所示:

<blank> no a no as no

So at this point both password and confirm fields have "asd", but I need to enter an extra character before that is recognized and the "disabled" property is removed from the button.所以此时passwordconfirm字段都有“asd”,但我需要在识别之前输入一个额外的字符,并从按钮中删除“禁用”属性。

I have tried using .change() instead of .keypress() but that doesn't fire until the input field loses focus, which is not what I want.我曾尝试使用.change()而不是.keypress()但直到input字段失去焦点才会触发,这不是我想要的。

I want the button on the page to be disabled until the password and confirm-password fields have matching values我希望页面上的按钮被禁用,直到密码和确认密码字段具有匹配的值

If this is your goal, you can add event listeners to both inputs that call a validation function:如果这是您的目标,您可以向调用验证函数的两个输入添加事件侦听器:

$('#password').on("input", function() { validatePassword(); });

$('#confirm').on("input", function() { validatePassword(); });

function validatePassword() {
    if($('#password').val() && $('#confirm').val() && $('#password').val() == $('#confirm').val()) {
        $('button').prop('disabled', false);
    } else {
        $('button').prop('disabled', true);
    }
}

It also may be worthwhile adding an ID to the button.向按钮添加 ID 也可能是值得的。 Using 'button' would enable/disable all elements on the page.使用“按钮”将启用/禁用页面上的所有元素。

Example: https://jsfiddle.net/doL4t9vv/1/示例: https : //jsfiddle.net/doL4t9vv/1/

I had the same problem few months ago.几个月前我遇到了同样的问题。
Try to use the keyup function from Jquery.尝试使用 Jquery 的keyup函数。

Keypress event is fired when you press the key, so the input is not fill yet.当您按下键时会触发 Keypress 事件,因此输入尚未填充。
Keyup event is fired when you release the key.当您松开按键时会触发 Keyup 事件。

Can use input event and simplify this down to可以使用input事件并将其简化为

var $passwords =$('#confirm, #password').on('input', function() {
    var thisValue = this.value.trim();
    // does this input have value and does it match other
    var isValid = thisValue  && thisValue  === $passwords.not(this).val().trim();
    // boolean used for disabled property    
    $('button').prop('disabled', !isValid);      
});

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

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