文字框焦點清空以及斷行符號鍵提交表單的實現

來源:互聯網
上載者:User

1、輸入框得到焦點,清空輸入框

很多頁面例如搜尋方塊等,需要當滑鼠焦點在輸入框中時,清空輸入框中內容,這個非常容易實現,請看下面代碼:
 <asp:textbox id="txb_KeyWord" MaxLength="20" Runat="server" Width="100px" EnableViewState="False" onFocus="this.value='';">請輸入關鍵字</asp:textbox>
僅僅只需要在該控制項代碼中加入一個屬性就可以,為:onFocus=this.value='';注意是兩個單引號,在前面完整的代碼中this.value='';又包括在雙引號中。

2、斷行符號鍵提交表單
ASP.NET頁面本身沒有辦法自己實現,這裡使用用戶端的javascript指令碼檢查使用者的每一次按鍵,如果為斷行符號,那麼提交表單,注意程式中兩個顏色不同的控制項名稱必須與你的ASPX頁面中的控制項的名稱完全一致,其中Form1為頁面表單的ID,btn_Select是需要斷行符號觸發的伺服器按鈕的名稱。
<script language="javascript">
    function KeyDownHandler()
    {
        // process only the Enter key
        if (event.keyCode == 13)
        {
            // cancel the default submit
            event.returnValue=false;
            event.cancel = true;
            // submit the form by programmatically clicking the specified button
            Form1.btn_Select.click();
        }
    }
</script>

下面是幾篇相關文章
Setting the default Button for a TextBox in ASP.NET
Submitting default buttons when the user presses the Enter key - finally!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.