• 回车提交


    在web页面中我们会碰到这样一种情况,如:

    <form id="form1" runat="server"  >
        <div>
            <textarea id="TextArea1" cols="20" rows="2"></textarea>
            <input id="Text1" type="text" />
        </div>
        </form>

    当我们在Text1中敲回车的时候,会出现form1被提交到后台服务器。有些时候,这种情况我们是不想出现的。

    所以我们给form添加onkeypress事件 <form id="form1" runat="server" onkeypress="if(event.keyCode==13||event.which==13){return false;}" >,此时当我们再在Text1中敲回车发现不会被提交,但是出现了另外一种情况,在TextArea1中敲回车不能换行了,这也是我们不希望看到的情况。解决方法如下,

    <script src="jquery-1.4.2.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        $(function (){$("textarea").keypress(function(event){event.stopPropagation(); })});   
        </script>

    说明需要引入jquery,此时再去敲回车在Text1和TextArea1中,会发现在Text1中敲回车不会提交form,在TextArea1敲回车可以换行。

    整个代码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
        <script src="jquery-1.4.2.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        $(function (){$("textarea").keypress(function(event){event.stopPropagation(); })});   
        </script>
    </head>
    <body>
        <form id="form1" runat="server"  onkeypress="if(event.keyCode==13||event.which==13){return false;}">
        <div>
            <textarea id="TextArea1" cols="20" rows="2"></textarea>
            <input id="Text1" type="text" />
        </div>
        </form>
    </body>
    </html>

  • 相关阅读:
    coalesce搭配nullif使用
    阿里云服务器数据备份到本地
    MSSQL 删除数据库表数据
    MSSQL 删除重复数据
    MSSQL 字段分组拼接
    MySql 字段分组拼接
    获取格式字符串第idx个值及实例
    针对字符串长度超过8000的处理
    【21】责任链模式
    【20】策略者模式(Strategy Pattern)
  • 原文地址:https://www.cnblogs.com/lorgine/p/1808256.html
Copyright © 2020-2023  润新知