Javascript 阻止在运输后提交表单 return

Javascript preventing submit of a form after carriage return

我正在使用条形码扫描器向某些 html 字段添加数据。我想做的是以下

  1. 关注第一个字段
  2. 扫描并在第一个字段中输入数据
  3. 将焦点切换到第二个字段
  4. 扫描并在第二个字段中输入数据
  5. 提交表单

我尝试了两种方法:

发布的是后一篇

它目前有效,但前提是我在代码中留下我的调试警报... 如果我删除它们,则提交表单...

知道为什么吗?

    <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>

好的,我在这个 post how to prevent buttons from submitting forms

的深处找到了解决方案

定义按钮类型后

<button type="button">Button</button>

效果很好。感谢您的支持:)