简体   繁体   English

jQuery .on()在给定值之前运行

[英]jQuery .on() running before value given

Why does .on() not get the value of the form input field (#forgot) before the keypress has happened. 为什么.on()在keypress发生之前没有得到表单输入字段(#forgot)的值。

$(document).ready(function() {
    $(document).on('keypress', '#pass', function() {
    var value = $.trim($('#pass').val());
        alert(value);
        if (!value.length) {
            alert("Show");
            $('#forgot').show();
        } else {
            alert("Hide");
            $('#forgot').hide();
        }
    });
});

When I type in the first character the alert shows no input. 当我输入第一个字符时,警报显示没有输入。 The second character leads to the value being only the first character. 第二个字符导致值只是第一个字符。 The .on() function seems to run before the key press is registered? .on()函数似乎在按键按下之前运行? How can I fix this or is there an alternative function? 我该如何解决这个问题还是有替代功能?

keypress event triggered when you press a key. keypress触发keypress事件。 It will not wait for the value to come. 它不会等待价值的到来。 Try with keyup . 尝试使用keyup keyup gets triggered when you a key is released after pressing, till that time the value is proccessed. 按下后释放键时触发keyup ,直到该值被执行为止。 - -

 $(document).on('keyup', '#pass', function() {

keypress 按键

The keypress event is sent to an element when the browser registers keyboard input. 当浏览器注册键盘输入时,按键事件将发送到元素。 This is similar to the keydown event, except that modifier and non-printing keys such as Shift, Esc, and delete trigger keydown events but not keypress events. 这类似于keydown事件,除了修饰符和非打印键(如Shift,Esc和delete)触发keydown事件,但不触发keypress事件。 Other differences between the two events may arise depending on platform and browser. 根据平台和浏览器,可能会出现两个事件之间的其他差异。

keyup KEYUP

The keyup event is sent to an element when the user releases a key on the keyboard. 当用户在键盘上释放键时,键盘事件被发送到元素。 It can be attached to any element, but the event is only sent to the element that has the focus. 它可以附加到任何元素,但事件仅发送到具有焦点的元素。 Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type. 可聚焦元素可以在浏览器之间变化,但是表单元素总是可以获得焦点,因此这种事件类型是合理的候选者。

Simply change keypress to keyup : 只需将keypress更改为keyup

$(document).ready(function() {
    $(document).on('keyup', '#pass', function() {
    var value = $.trim($('#pass').val());
        console.log(value);
        if (!value.length) {
            console.log("Show");
            $('#forgot').show();
        } else {
            console.log("Hide");
            $('#forgot').hide();
        }
    });
});

You need the keyup event instead of the keypress. 您需要keyup事件而不是keypress。

So replace: 所以替换:

 $(document).on('keypress', '#pass', function() {

With

 $(document).on('keyup', '#pass', function() {

keypress event is triggered when the key is pressed before the key is up). 在键启动之前按下键时触发按键事件)。 So it won't get the complete value. 所以它不会得到完整的价值。 Use keyup 使用keyup

JSFIDDLE DEMO JSFIDDLE DEMO

HTML HTML

<input id="pass">

JQUERY JQUERY

$(document).ready(function() {
    $(document).on('keyup', '#pass', function() {
    var value = $.trim($('#pass').val());
        alert(value);
        if (!value.length) {
            alert("Show");
            $('#forgot').show();
        } else {
            alert("Hide");
            $('#forgot').hide();
        }
    });
});

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

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