简体   繁体   中英

jquery alternative to change or keyup

I have a form in which the user must initial a bunch of different sections.

<script>
  $.function(){
    $('.initialMe').change(function(){
      var id = $(this).attr('id');
      alert(id);
    }); 
  });
</script>

<?php
  echo "<div class='signature'>Initial Here:<input type='text' class='initialMe' id='sign[service1]' name='sign[service1]' size='4'></div>";
  echo "<div class='signature'>Initial Here:<input type='text' class='initialMe' id='sign[service2]' name='sign[service2]' size='4'></div>";
?>

I want to fire off an action when they are done initializing the text box. I don't want to use change because they have to click off somewhere for event to be fired. I don't want to use keyup , because event will fire after first letter of initials. I do not know if user will use 2 letters or 3 for initials. This is not a submittable form. Think of it as a legal document where the user has to initial different sections of the page. What options do I have?

You can use a combination of keyup and setTimeout . What I am trying to say is, bind it in keyup and give some time for the user to feel like it's done and then check.

 $(function () { var tmr = 0; $("input").keyup(function () { clearTimeout(tmr); tmr = setTimeout(function () { alert("Checking..."); }, 1000); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input /> 

This way, it is checked every time, but gives some time. When the user types again within a second, it clears the previous timer, so the check will not happen.

Why not do it on blur() ?

If the user has clicked or tabbed away from the field then that gives an indication they're done imho.

Another option, if it's the same user initialing several parts of a form - you could wait until the first field is done, check with the user that it's correct, then don't do anything with the others until then initials match the first completed field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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