简体   繁体   English

jQuery替代更改或键入

[英]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. 我不想使用keyup ,因为event将在首字母缩写后触发。 I do not know if user will use 2 letters or 3 for initials. 我不知道用户是否将2个字母或3个字母用作缩写。 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 . 您可以结合使用keyupsetTimeout 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. 我要说的是,将其绑定到keyup并给用户一些时间以使其感觉已完成,然后进行检查。

 $(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() ? 为什么不使用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. 另一种选择是,如果是同一用户对表单的几个部分进行初始化-您可以等到第一个字段完成后,再与用户核对是否正确,然后对其他用户不做任何操作,直到缩写匹配第一个完成的字段。

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

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