我遇到了一些麻烦,我用 https://github.com/mailcheck/mailcheck
有这个功能
$('#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()
}
});
});
但是当脚本使用时:
opts.email = this.val();
(此文件的第 263 行 https://github.com/mailcheck/mailcheck/blob/master/src/mailcheck.js)
this.val() 返回我输入的旧值。 我试过使用
this.attr("value");
和
this.prop("value");
同样的问题。
当我执行 console.log(this);并在对象中寻找我输入的值,它很好,它包含好的值。
那如何得到合适的值呢? 这就是我请求您帮助的原因。
谢谢。
最佳答案
不要使用 keypress
事件,因为它在新的 value
之前被触发或 character
注册于 input
.使用 keyup相反
$('#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()
}
});
});
关于javascript - JQuery - .val() 返回旧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30706808/