简体   繁体   English

用户完成从第一个输入文本的键入后如何设置和聚焦最后一个输入文本

[英]How to Set & Focus the last input text once user finishes typing from the 1st input text

I have multiple input text inside a table. 我在表中有多个输入文本。 How can I automatically set value & focus the last input text once I'm done typing values from the 1st input text box? 在第一个输入文本框中输入值后,如何自动设置值并集中最后输入的文本?

<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>
<td> <input type="text"/> </td>

my js code: 我的js代码:

var d =  id('my_table', document); 
for (var x=1; x<d.rows.length; x++) {                    
    d.rows[x].cells[12].firstChild.innerText = x;                   
    d.rows[x].cells[12].firstChild.focus();     
}

You could handle the blur event or the keypress event (to handle that the user pressed Enter ) or a combination of both: 您可以处理blur事件或keypress事件(以处理用户按下Enter的事件 )或两者的组合:

 var inputs = document.querySelectorAll('#table input'); inputs[0].addEventListener('blur', setFocus); inputs[0].addEventListener('keypress', function(e) { if (e.keyCode === 13) setFocus(); }); function setFocus() { inputs[inputs.length - 1].value = '123'; inputs[inputs.length - 1].focus(); } 
 <table id="table"> <tr><td><input type="text" ></td></tr> <tr><td><input type="text" ></td></tr> <tr><td><input type="text" ></td></tr> </table> 

I am not sure, It the same as your want but its helps you, try this 我不确定,它与您想要的一样,但可以帮助您,请尝试此操作

<script src="http://code.jquery.com/jquery.min.js"></script>
<td> <input type="text" maxlength="4" tabindex="1" /> </td>
<td> <input type="text" maxlength="4" tabindex="3" /> </td>
<td> <input type="text" maxlength="4" tabindex="4"/> </td>
<td> <input type="text" maxlength="4" tabindex="2" /> </td>
<script type="text/javascript">
jQuery("input").on('input',function () {
    if(jQuery(this).val().length == jQuery(this).attr('maxlength')) {
        nextinput = parseInt(jQuery(this).attr('tabindex'))+1;
        // Here you can take current input value and set where you want
        jQuery("input[tabindex="+nextinput+"]").focus();
    }
});
</script>

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

相关问题 javasccript键入时如何大写输入字段文本的第一个字母? - how to capitalize the 1st letter of input field text while typing by javasccript? 在将焦点输入到下一个文本输入后 - after typing focus to next text input 如果其他输入文本没有焦点并且具有价值但如果我集中并输入文本仍然存在,如何删除文本 - how to delete a text if other input text not focus and have value but if i focus and typing the text still there 如何在asp.net MVC中参考第一个文本框的输入添加另一个文本框 - How to add another text box in reference to the 1st text box's input in asp.net mvc 仅当用户开始在输入字段中输入时,如何从输入值中删除某些警告文本? - How to remove certain warning text from input value only when user starts typing in the input field? 如果第一位和第三位相等,如何检查用户输入 - How to check user input if 1st and 3rd digit are equal 如何在输入焦点上突出显示文本? - How to highlight text on input focus? 输入字符后反应输入文本框失去焦点 - React input text box loses focus after typing a character 如何将焦点设置为文本输入模式 - How Can I set Focus on a text Input Modal 在刚刚创建的窗口中设置文本输入的焦点 - Set focus for text input in just created window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM