简体   繁体   English

为什么我不能使用jQuery的.keypress()、. keydown()和.keyup()事件正确比较两个字符串?

[英]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中的值将与您看到的值相同

You need to use keyup , because its triggert after keypress . 您需要使用keyup ,因为它是在keypress后触发的。 You can see this for example here 您可以here看到例如

$( "#verify-password" ).keyup( function() {

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

相关问题 keyup、keydown 和 keypress 事件在移动设备上不起作用 - keyup, keydown and keypress events not working on mobile 在 JS/jQuery 中触发 keypress/keydown/keyup 事件? - Trigger a keypress/keydown/keyup event in JS/jQuery? 为什么只有 keydown 有效,而 keyup 或 keypress 无效? - Why does only keydown work, and not keyup or keypress? jQuery keypress()事件不适用于keyup() - jquery keypress() events are not working for keyup() 无法通过使用.keyup,.keydown和.keypress实现解决方案 - Unable to implement solution by using .keyup, .keydown ,.keypress jQuery,如果keydown和keyup事件触发器禁用了其他按键事件,则无法在IE和Mozilla中使用Chrome - Jquery if keydown and keyup event trigger disable other keypress events works in Chrome not in IE & Mozilla javascript event.keyCode不适用于FireFox 3.6.25中的keydown,keypress或keyup事件 - javascript event.keyCode doesn't work on keydown, keypress or keyup events in FireFox 3.6.25 按键,按键和按键的计时 - timing with keyup, keydown, and keypress Android KeyUp,KeyDown和KeyPress事件不适用于Angular2 - Android KeyUp, KeyDown, and KeyPress events not working with Angular2 处理仅产生按键而不产生按键按下/按键事件的键盘 - Handle a keyboard that produces only keypress instead of keydown/keyup events
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM