2015-11-24 28 views
0

我正在使用条形码扫描器将数据添加到某些html字段。我想要做的是以下Javascript防止在回车后提交表单

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

你可以把你的代码放在jsfiddle中吗? –

+0

将'onsumbit ='return processForm(window.event);“'添加到您的'

'标记中,并验证这两个字段是否都已实际填充。另一方面,根据需要设置输入,并为它们提供一种模式以验证它们也应该做的伎俩,然后你不必在Javascript中进行验证 – Icepickle

+0

@HarshSanghani有[snipplets](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and -html-code-snippets /)在stackoverflow上。 jsfiddle没有理由。 – k0pernikus

回答