使用条形码扫描器将焦点从一个输入切换到下一个输入
Changing focus from one input to the next with a barcode scanner
我正在使用条形码扫描仪将数据输入网页上的输入字段。我在第一个输入字段上设置了表单和自动对焦。当第一个条形码输入第一个输入字段时,我希望焦点跳转到下一个输入字段。但是,当我输入第一个条形码时,表格 'submits'。这是我正在使用的 html:
<form id="barcode1" name="barcode" method="Post" action="#">
<div>
<label for="S1">Barcode 1 </label>
<input id="S1" class="bcode" type="text" name="S1" autofocus/>
<label for="S2">Barcode 2 </label>
<input id="S2" class="bcode" type="text" name="S2" />
<label for="S3">Barcode 3 </label>
<input id="S3" class="bcode" type="text" name="S3" />
</div>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
我尝试过类似 SO 问题的解决方案 here and [here] (http://jsfiddle.net/davidThomas/ZmAEG/),但它们似乎不起作用。
理想情况下,我希望有一个类似这样的解决方案(上面的第二个 link),所以请告诉我它在哪里或为什么不起作用,以及我可以做些什么来修复它。
$('form').on('keypress','input',function(e){
var eClassName = this.className,
index = $(this).index('.' + eClassName) + 1;
if (e.which === 13){
e.preventDefault();
$('input.' + eClassName)
.eq(index)
.focus();
}
});
如果您的条码扫描器是键盘楔形,您应该能够将尾随字符配置为制表符。
看起来,默认情况下,您的扫描仪尾随一个 ENTER(回车 return)。
另一种选择是在您的 javascript 代码中检查 LF(十进制 10)。
您需要 return false 以防止回车键提交表单。
这个问题的答案对你有帮助:Prevent form submission with enter key
//Press Enter in INPUT moves cursor to next INPUT
$('#form').find('.input').keypress(function(e){
if ( e.which == 13 ) // Enter key = keycode 13
{
$(this).next().focus(); //Use whatever selector necessary to focus the 'next' input
return false;
}
});
无需对条形扫描仪进行任何更改。
看起来您的函数永远不会被调用,因为浏览器会在 Enter 上提交表单。您可能必须通过首先拦截提交事件来抑制提交,直到所有字段都被填充(或某些其他条件)。
$( "form" ).submit(function( event ) {
if(/*all req. fields are filled -> submit the form*/)
$(this).submit();
event.preventDefault();
});
我正在使用条形码扫描仪将数据输入网页上的输入字段。我在第一个输入字段上设置了表单和自动对焦。当第一个条形码输入第一个输入字段时,我希望焦点跳转到下一个输入字段。但是,当我输入第一个条形码时,表格 'submits'。这是我正在使用的 html:
<form id="barcode1" name="barcode" method="Post" action="#">
<div>
<label for="S1">Barcode 1 </label>
<input id="S1" class="bcode" type="text" name="S1" autofocus/>
<label for="S2">Barcode 2 </label>
<input id="S2" class="bcode" type="text" name="S2" />
<label for="S3">Barcode 3 </label>
<input id="S3" class="bcode" type="text" name="S3" />
</div>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
我尝试过类似 SO 问题的解决方案 here and [here] (http://jsfiddle.net/davidThomas/ZmAEG/),但它们似乎不起作用。
理想情况下,我希望有一个类似这样的解决方案(上面的第二个 link),所以请告诉我它在哪里或为什么不起作用,以及我可以做些什么来修复它。
$('form').on('keypress','input',function(e){
var eClassName = this.className,
index = $(this).index('.' + eClassName) + 1;
if (e.which === 13){
e.preventDefault();
$('input.' + eClassName)
.eq(index)
.focus();
}
});
如果您的条码扫描器是键盘楔形,您应该能够将尾随字符配置为制表符。
看起来,默认情况下,您的扫描仪尾随一个 ENTER(回车 return)。
另一种选择是在您的 javascript 代码中检查 LF(十进制 10)。
您需要 return false 以防止回车键提交表单。
这个问题的答案对你有帮助:Prevent form submission with enter key
//Press Enter in INPUT moves cursor to next INPUT
$('#form').find('.input').keypress(function(e){
if ( e.which == 13 ) // Enter key = keycode 13
{
$(this).next().focus(); //Use whatever selector necessary to focus the 'next' input
return false;
}
});
无需对条形扫描仪进行任何更改。
看起来您的函数永远不会被调用,因为浏览器会在 Enter 上提交表单。您可能必须通过首先拦截提交事件来抑制提交,直到所有字段都被填充(或某些其他条件)。
$( "form" ).submit(function( event ) {
if(/*all req. fields are filled -> submit the form*/)
$(this).submit();
event.preventDefault();
});