Javascript 忽略“回车”键,在文本输入5个字符后移动焦点
我的客户需要扫描一个条形码,其中包含发票号码5位数字和“回车”键 我需要输入框忽略“回车”键,并将焦点移动到5个字符后的下一个输入框Javascript 忽略“回车”键,在文本输入5个字符后移动焦点,javascript,jquery,html,Javascript,Jquery,Html,我的客户需要扫描一个条形码,其中包含发票号码5位数字和“回车”键 我需要输入框忽略“回车”键,并将焦点移动到5个字符后的下一个输入框 如果您能提供一些示例代码,我将不胜感激。谢谢 使用文本框的onkeypress或onkeyup并添加代码ifevent.keyCode==13返回false;13是回车键,将焦点设置为下一个控件 例如,您有两个文本框TxtBox1和TxtBox2 <input id="TxtBox1" type="submit" onkeypress="if(event.
如果您能提供一些示例代码,我将不胜感激。谢谢 使用文本框的onkeypress或onkeyup并添加代码ifevent.keyCode==13返回false;13是回车键,将焦点设置为下一个控件 例如,您有两个文本框TxtBox1和TxtBox2
<input id="TxtBox1" type="submit" onkeypress="if(event.keyCode==13)return false;document.getElementById('TxtBox2').focus(); " name="Submit" value="Send" />
使用文本框的onkeypress或onkeyup并添加代码ifevent.keyCode==13返回false;13是回车键,将焦点设置为下一个控件 例如,您有两个文本框TxtBox1和TxtBox2
<input id="TxtBox1" type="submit" onkeypress="if(event.keyCode==13)return false;document.getElementById('TxtBox2').focus(); " name="Submit" value="Send" />
一些JQuery:
$('#test').keydown(function(e) {
if (e.which == 13) {
if ($(this).val().length == 5) {
e.preventDefault();
$('#test2').focus();
}
}
});
假设“test”和“test2”是两个文本框的id
$('#test').keydown(function(e) {
if (e.which == 13) {
if ($(this).val().length == 5) {
e.preventDefault();
$('#test2').focus();
}
}
});
$('input[type=text]').on('keyup', function(e) {
if (e.which != 27 || e.which != 8) { // check for backspace and delete
if (e.which != 13) { // ignoring enter
if (!this.value.replace(/\s/g, '').match(/\d/g)) { // checking for digit
alert('Insert Digit');
$(this).val(''); // make the field empty
} else {
if (this.value.length > 4) {
$(this).next().focus(); // automatically change current input of length 5 digits
}
}
}
}
});
假设“test”和“test2”是两个文本框的id
$('input[type=text]').on('keyup', function(e) {
if (e.which != 27 || e.which != 8) { // check for backspace and delete
if (e.which != 13) { // ignoring enter
if (!this.value.replace(/\s/g, '').match(/\d/g)) { // checking for digit
alert('Insert Digit');
$(this).val(''); // make the field empty
} else {
if (this.value.length > 4) {
$(this).next().focus(); // automatically change current input of length 5 digits
}
}
}
}
});
不要忽略enter键,而是将其用作移动对象的提示。大致如下:
$("#myTextbox").keydown(function(event){
if(event.keyCode == 13){
// validate input and move on to the next textbox
}
});
不要忽略enter键,而是将其用作移动对象的提示。大致如下:
$("#myTextbox").keydown(function(event){
if(event.keyCode == 13){
// validate input and move on to the next textbox
}
});
这很有效。我根据这篇[文章]提出了一个解决方案。然而,这个解决方案更加全面和有用。[文章]:效果很好。我根据这篇[文章]提出了一个解决方案。然而,这个解决方案更加全面和有用。[文章]: