[英]Why I can't compare two strings correctly using jQuery's .keypress(), .keydown() and .keyup() events?
I want to validate two inputs using javascript
. 我想使用
javascript
验证两个输入。 I am using jQuery's .keypress()
events to directly verify the inputted values. 我正在使用
jQuery's .keypress()
事件直接验证输入的值。 Here's my code in laravel
framework: 这是我在
laravel
框架中的代码:
HTML HTML
<div class="form-group">
<label for="inputPassword"><h4>Password:</h4></label>
{{ Form::password('password',array('class'=>'span3 form-control', 'id'=>'password', 'placeholder'=>'Password')) }}
</div>
<div class="form-group">
<label for="verifyPassword"><h4>Verify Password:</h4></label>
{{ Form::password('password',array('class'=>'span3 form-control', 'id'=>'verify-password', 'placeholder'=>'Verify Password')) }}
</div>
<div class="form-group">
<div class="alert alert-danger hide" id="alert-verify-password-remove">
Password don't match!
<div class="glyphicon glyphicon-remove alert-icon-padding-remove"></div>
</div>
</div>
<div class="form-group">
<div class="alert alert-success hide" id="alert-verify-password-ok">
Password matched!
<div class="glyphicon glyphicon-ok alert-icon-padding-ok"></div>
</div>
</div>
JS JS
$(document).ready(function(){
$( "#verify-password" ).keypress(function() {
// get password value from first password field
var pwd = $('#password').val();
// get the 2nd password value from the verify password field
var vPwd = $('#verify-password').val();
// verify the values if they are matched
// if matched then show match alert | hide unmatch alert
if (pwd == vPwd) {
$("#alert-verify-password-ok").removeClass('hide');
$("#alert-verify-password-remove").addClass('hide');
} // else, show unmatch alert | hide match alert
else {
$("#alert-verify-password-remove").removeClass('hide');
$("#alert-verify-password-ok").addClass('hide');
}
});
});
The problem I got is that, If for example I typed on #password
field is test
then in #verify-password
field I have to type tests
or any character after the 4th before the validation validates it to be correct/matched. 我遇到的问题是,例如,如果我在
#password
字段上键入的是test
那么在#verify-password
字段中,我必须在第4个字符之后键入tests
或任何字符,然后验证才能验证其正确/匹配。 Here's the picture that shows what is happening. 这是显示正在发生的事情的图片。
As you can see, I have to type 5 ( tests
) characters instead of 4 ( test
) before it identify that the values are the same. 正如你所看到的,我必须键入5(
tests
)的字符,而不是4( test
)之前确定的值相同。 Can someone give me insights what's happening here and at least give me suitable solutions for this. 有人可以给我提供见解吗,至少可以给我合适的解决方案。 Thank you.
谢谢。
Because keypress
occurs before the string is actually changed (And thus you can cancel string change) 因为
keypress
发生在实际更改字符串之前(因此您可以取消更改字符串)
Use keyup
event for those tests 对这些测试使用
keyup
事件
...
$( "#verify-password" ).keyup( function() {
...
keyup
is fired after string is changed, so the values in test will be the same as the values you see 更改字符串后会触发
keyup
,因此test中的值将与您看到的值相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.