0
我正在使用条形码扫描器将数据添加到某些html字段。我想要做的是以下Javascript防止在回车后提交表单
- 注重第一场
- 扫描和数据输入到第一场
- 开关将焦点转移到第二场
- 扫描和数据输入到第二场
- 提交表单
我尝试两种方法:
- 捕获的手扫描器
- 发送回车捕获每个按键的文本字段
发布是后者。
它的工作中辛勤,但只有当我离开我的调试警报代码... 如果删除它们,提交表单...
任何想法,为什么?
<html>
<head>
<head>
<title>Webinterface</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<script type="text/javascript">
function processForm(e) {
if (e.preventDefault) e.preventDefault();
return false;
}
function checkFieldC(text)
{
if(text.length==6){
alert("C1");
if(document.comform.Destination.value.length>0){
alert("C2a");
document.forms["comform"].submit();
return true;
}else{
alert("C2b");
document.comform.Destination.focus();
return false;
}
}
return false;
}
function checkFieldD(text)
{
if(text.length==9){
if(document.comform.Container.value.length>0){
alert("D2a");
document.forms["comform"].submit();
return true;
}else{
alert("D2b");
document.comform.Container.focus();
return false;
}
}
return false;
}
</script>
<form method="POST" name="comform" action="DoSomething.php">
<br>
<table width="90%" border="0">
<tr>
<td valign="top" width="150">Container (6 digits)</td>
<td><input name="comvalue[]" type="text" id="Container" size="10" maxlength="6" onkeyup="checkFieldC(this.value)" ></td>
</tr>
<br>
<tr>
<td valign="top" width="150">Destination:</td>
<td><input name="comvalue[]" type="text" id="Destination" size="10" onkeyup="checkFieldD(this.value)"></td>
</tr>
<tr>
<td>Confirm</td>
<td><button name="s2" onClick="submit()" class="cssButton1">Confirm</button></td>
</tr>
</table>
</font>
</form>
</body>
</html>
你可以把你的代码放在jsfiddle中吗? –
将'onsumbit ='return processForm(window.event);“'添加到您的'
@HarshSanghani有[snipplets](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and -html-code-snippets /)在stackoverflow上。 jsfiddle没有理由。 – k0pernikus