ASP. NET page responds to the carriage return event in the text box with JavaScript

Source: Internet
Author: User

The search function of many shopping websites is that you enter something in the text box and press enter to jump to the search page. However, if you press ENTER outside the text box, you will not respond.CodeFor example, the Zhongguancun mall is written in PHP, And the search is a form, which contains a textbox and a submit button. In this way, after pressing the carriage return button in the textbox, it will be automatically submitted to the action in the form, ASP. net write, a page is a form, write a response text box, press enter, and test, IE6, 8, Firefox, chrome browsing normal, code:

<% @ 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 ){
// Return carriage return
VaR Key = Window. Event ? E. keycode: E. Which;
If (Key =   13 ){
// Alert ("Press ENTER ");
Search ();
}
}
Function Search (){
// Search
VaR Key = Document. getelementbyid ( " Key " ). Value;
If (Key. Length =   0 ){
Document. getelementbyid ( " Key " ). Focus (); // Invalid
Alert ( " Enter a search keyword! " );
} Else {
VaR URL =   " Searchitem. aspx? Key = "   + Encodeuri (key) +   " & Amp; typeid = 0 & amp; typename = All Categories " ;
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" >
Search
</ Button >
< Div > <% = Datetime. Now %> </ Div >
  </ Form >
</ Body >
</ Html >

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.