ASP.NET頁面中用javascript響應文字框的斷行符號事件

來源:互聯網
上載者:User

     很多購物網站的搜尋功能,都是你在文字框中輸入東西後直接斷行符號就跳到搜尋網頁了,但是在文字框外按斷行符號是不會響應的,自己看了一下他們的代碼,像中關村商城的是PHP寫的,搜尋就是一個form,裡面一個textbox一個submit按鈕,這樣在textbox中按下斷行符號後會自動的提交到form中的action,自己是用ASP.NET寫的,一個頁面就一個form,自己寫了一個響應文字框中斷行符號的東西出來,自己測試過,IE6,8,FIREFOX,CHROME中瀏覽正常,代碼:

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

<!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></title>

    <script type="text/javascript">
        function responseEnter(e) {
            // 響應斷行符號
            var key = window.event ? e.keyCode : e.which;
            if (key == 13) {
                //alert("斷行符號了");
                search();
            }
        }
        function search() {
            // 搜尋
            var key = document.getElementById("key").value;
            if (key.length == 0) {                
                document.getElementById("key").focus(); //無效
                alert("請輸入搜尋索引鍵!");
            } else {
                var url = "SearchItem.aspx?key=" + encodeURI(key) + "&typeid=0&typename=所有分類";
                window.open(url);
            }
        }
    </script>

</head>
<body>
 <form id="form1" runat="server" onsubmit="return false;">
    <input id="key" type="text" onkeypress="responseEnter(event);" />
    <button style="cursor: pointer;" onclick="search();" type="button">
        搜尋
    </button>
    <div><%=DateTime.Now %></div>
 </form>
</body>
</html>

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.