Form submit when enter button is pressed

Results 1 to 3 of 3

Thread: Form submit when enter button is pressed

  1. #1
    Join Date
    Dec 1969

    Default Form submit when enter button is pressed

    First the problem: I have a page that has two textboxes and 5 buttons, each doing different things. The problem is that when the user types in the textbox and presses the enter key, it submits the wrong button. How do I get it to submit the correct button? This is the code I have so far:<BR><BR>&#060;SCRIPT language="javascript" type=text/javascript&#062;<BR> function KeyDownHandler(btn)<BR> {<BR> // process only the Enter key<BR> if (event.keyCode == 13)<BR> {<BR> // cancel the default submit<BR> event.returnValue=false;<BR> event.cancel = true;<BR> // submit the form by programmatically clicking the specified button<BR>;<BR> }<BR> }<BR>&#060;/script&#062;<BR><BR><BR>&#060;asp:textbox id="txtPassword" tabIndex="2" runat="server" Width="100px" TextMode="Password" onKeyDown="KeyDownHandler(btnPC)"&#062;&#060;/asp:textbox&#062;<BR><BR>I thought this would work, but it&#039;s not. Any suggestions?

  2. #2
    Join Date
    Dec 1969

    Default this is what i have in an app

    and it works perfectly<BR><BR>[code language="VB.NET"]&#060;input type="text" class="QuikcSearchText" onKeyDown="return QuickPress(this);" name="txtQuickSearch" id="txtQuickSearch" /&#062;[/code]<BR><BR>[code language="C#"]function QuickPress(obj) {<BR> var txt = trim(document.getElementById(&#039;txtQuickSearch& #039;).value);<BR> if (event.keyCode == 13 && txt != &#039;&#039;) {<BR> return OpenQuckSearch(txt,true);<BR> }<BR> return true;<BR>}[/code]<BR><BR>in which the function "OpenQuckSearch" opens a popup and returns "false" so it stops the enter key (and the event raised by it) dead in its tracks<BR><BR>now that i compare the two, we&#039;re doing it totally different ways, but mine works :) lol<BR><BR>maybe it&#039;ll help none the less

  3. #3
    Join Date
    Dec 1969

    Default RE: Form submit when enter button is pressed

    Is your function not being called? I&#039;d guess that the onKeyDown="KeyDownHandler(btnPC)" isn&#039;t being rendered. Check your source when the page is loaded. You may need to add this attribute from the code behind...<BR><BR>txtPassword.Attributes.Add("onKey Down", "KeyDownHandler(btnPC)");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts