WEB开发中,如果页面的 form 中只有一个input元素,在该input元素的输入框中按回车(注:此时并没有写对应的onkeydown等事件处理),则浏览器会默认提交表单,请看如下代码:

<html>  
    <head>  
       <title>页面中只有一个input元素时默认提交表单</title>  
    </head>  
    <body>  
         <form action="http://www.baidu.com" name="myform" method="get">  
            <input name="contenta" type="text" value="回车提交" /></br>  
         </form>  
    </body>  
</html>  

那么如何禁止呢,禁止方法有两种:

[1]只有一个时发生,可以在form中添加一个隐藏的input元素,修改后的代码如下:

<html>  
    <head>  
       <title>页面中只有一个input元素时默认提交表单</title>  
    </head>  
    <body>  
         <form action="http://www.baidu.com" name="myform" method="get">  
            <input name="contenta" type="text" value="回车提交" /></br>
            <input type="text" style="display:none">  
         </form>  
    </body>  
</html>  

[2]禁用form的onsubmit事件:

<html>  
    <head>  
        <title>页面中只有一个input元素时默认提交表单</title>  
    </head>  
    <body>  
         <form onsubmit="return false;"action="XXX" name="myform" method="get">  
            <input name="contenta" type="text" value="Enter To Submit" /></br>  
         </form>  
    </body>  
</html> 
<script type="text/javascript">
function update() {
  document.forms[0].action="${contextPath}UpdateAction.action";
  document.forms[0].submit();
}
</script>

 

相关文章:

  • 2021-10-25
  • 2022-12-23
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2021-11-27
猜你喜欢
  • 2021-07-17
  • 2022-12-23
  • 2022-03-08
  • 2021-11-27
  • 2022-12-23
  • 2021-11-10
  • 2022-12-23
相关资源
相似解决方案