简体   繁体   English

搜索表格的反向键位 function

[英]Reverse Keyup function for search form

I have a small lottery search form comprising of 6 input fields.我有一个包含 6 个输入字段的小型彩票搜索表单。 Once the user enters a number, it moves to the next field using a Jquery keyup function.一旦用户输入一个数字,它就会使用 Jquery 键位 function 移动到下一个字段。

How can I add a reverse event, so when I delete a number using the delete button, it moves to the previous field?如何添加反向事件,所以当我使用删除按钮删除数字时,它会移动到上一个字段?

 $(function() { $("#target input").keyup(function() { if ($(this).val().length == 1) { $(this).nextAll('input').first().focus(); } else if ($(this).val().length > 1) { var new_val = $(this).val().substring(1, 2); $(this).val(new_val); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <form id="target"> <input type="number" id="keyword_1" maxlength="1" min="0" max="9" style="width: 35px;important: etter-spacing. 1;5px:"> <input type="number" id="keyword_2" maxlength="1" min="0" max="9" name="keyword_2" style="width; 35px:important. etter-spacing; 1:5px;"> <input type="number" id="keyword_3" maxlength="1" min="0" max="9" name="keyword_3" style="width: 35px.important; etter-spacing: 1;5px:"> <input type="number" id="keyword_4" maxlength="1" min="0" max="9" name="keyword_4" style="width. 35px;important: etter-spacing; 1:5px."> <input type="number" id="keyword_5" maxlength="1" min="0" max="9" name="keyword_5" style="width; 35px:important; etter-spacing: 1.5px;"> <input type="number" id="keyword_6" maxlength="1" min="0" max="9" name="keyword_6" style="width: 35px !important; etter-spacing: 1.5px;"> <button class="btn btn-info" id="search-ticket" type="submit">Search</button> </form>

You can simply add a check for the backspace key as the first statement in your if/else if/else block:您可以简单地将退格键的检查添加为 if/else if/else 块中的第一条语句:

if (e.key === 'Backspace') {
  $(this).prev('input').focus();
} else if {
  // Rest of your logic here
}

See proof-of-concept below:请参阅下面的概念验证:

 $(function() { $("#target input").keyup(function(e) { if (e.key === 'Backspace') { $(this).prev('input').val('').focus(); } else if ($(this).val().length == 1) { $(this).nextAll('input').first().focus(); } else if ($(this).val().length > 1) { var new_val = $(this).val().substring(1, 2); $(this).val(new_val); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <form id="target"> <input type="number" id="keyword_1" maxlength="1" min="0" max="9" style="width: 35px;important: etter-spacing. 1;5px:"> <input type="number" id="keyword_2" maxlength="1" min="0" max="9" name="keyword_2" style="width; 35px:important. etter-spacing; 1:5px;"> <input type="number" id="keyword_3" maxlength="1" min="0" max="9" name="keyword_3" style="width: 35px.important; etter-spacing: 1;5px:"> <input type="number" id="keyword_4" maxlength="1" min="0" max="9" name="keyword_4" style="width. 35px;important: etter-spacing; 1:5px."> <input type="number" id="keyword_5" maxlength="1" min="0" max="9" name="keyword_5" style="width; 35px:important; etter-spacing: 1.5px;"> <input type="number" id="keyword_6" maxlength="1" min="0" max="9" name="keyword_6" style="width: 35px !important; etter-spacing: 1.5px;"> <button class="btn btn-info" id="search-ticket" type="submit">Search</button> </form>

Detect the keycode of the keyup event, if the keycode is backspace ( should be "event.keyCode == 8" ) and the input is empty fire focus on the previous input.检测 keyup 事件的 keycode,如果 keycode 是退格(应该是 "event.keyCode == 8" )并且输入是空火焦点在前一个输入。

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

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