简体   繁体   English

JQuery - .val()返回旧值

[英]JQuery - .val() return old value

I've got a some trouble, i use https://github.com/mailcheck/mailcheck 我有一些麻烦,我使用https://github.com/mailcheck/mailcheck

with this function 有了这个功能

                $('#email').on('keypress', function(event) {
                $(this).mailcheck({
                    suggested: function(element, suggestion) {
                        $('#email_check').html("Did you mean <b><i><a href='#' id='suggest_email'>" + suggestion.full + "</a></b></i>?");
                        $('#suggest_email').on('click', function(event) {
                            event.preventDefault();
                            $('#email').val($(this).text());
                            $('#email_check').empty();
                            $('.error_js').empty()
                        });
                    },
                    empty: function(element) {
                        $('#email_check').empty();
                        $('.error_js').empty()
                    }
                });
            });

But when the script use : 但是当脚本使用时:

opts.email = this.val(); 

(line 263 of this file https://github.com/mailcheck/mailcheck/blob/master/src/mailcheck.js ) (该文件的第263行https://github.com/mailcheck/mailcheck/blob/master/src/mailcheck.js

this.val() return the old value of my input. this.val()返回输入的旧值。 i've tried to use a 我试过用一个

this.attr("value");

and

this.prop("value");

Same problem. 同样的问题。

When i do a console.log(this); 当我做一个console.log(这个); and look for the value of my input, in the object, it's good, it contain the good value. 并且在对象中寻找我输入的值,它很好,它包含了很好的价值。

How to get the proper value then ? 那么如何获得适当的价值呢? That's why i request your help. 这就是我请求你帮助的原因。

Thanks. 谢谢。

Do not use the keypress event because it is fired before the new value or character is registered in the input . 不要使用keypress事件,因为它是在input注册新valuecharacter之前触发的。 Use keyup instead 请改用密钥

$('#email').on('keyup', function(event) {
                $(this).mailcheck({
                    suggested: function(element, suggestion) {
                        $('#email_check').html("Did you mean <b><i><a href='#' id='suggest_email'>" + suggestion.full + "</a></b></i>?");
                        $('#suggest_email').on('click', function(event) {
                            event.preventDefault();
                            $('#email').val($(this).text());
                            $('#email_check').empty();
                            $('.error_js').empty()
                        });
                    },
                    empty: function(element) {
                        $('#email_check').empty();
                        $('.error_js').empty()
                    }
                });
            });

The console log object show the object properties at the time the object was first expanded, so in your case the value is not yet being set when the keypress event triggers, but when you read it later in the console, it's set. 控制台日志对象在首次展开对象时显示对象属性,因此在您的情况下,当按键事件触发时,该值尚未设置,但是稍后在控制台中读取时,它已设置。 To see the actual current state of the object property, log that property only instead of the whole object . 要查看对象属性的实际当前状态,请仅记录该属性而不是整个对象

The keyup event should work fine instead. keyup事件应该可以正常工作。

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

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