views:

74

answers:

5

Hi,

I am facing one problem in the following code , Form is going to submit when i press enter in Keyword text field. there no such enter event bind in java script. Still this problem is happing

But when I remove the <form> tag it works fine ,

Please help .. if there is any mistake

<form action="" method="post" id="businessSearchForm" name="businessSearchForm">
                <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0" HEIGHT="100%" WIDTH="100%" CLASS="searchproperty">
                    <TR>
                        <TD ALIGN="left" COLSPAN="4" class="boldText">&nbsp;Business Search</TD>
                    </TR>
                    <TR>
                        <TD ALIGN="left" WIDTH="20%" class="boldText">&nbsp;Search For</TD>
                        <TD WIDTH="25%"><INPUT TYPE="radio" NAME="searchfor" VALUE="1" checked="checked">Product&nbsp;<INPUT TYPE="radio" NAME="searchfor" VALUE="2">Company</TD>
                        <TD ALIGN="left" WIDTH="20%" class="boldText">&nbsp;Business Type</TD>
                        <TD WIDTH="30%">&nbsp;<SELECT NAME="businessType" ID="businessType" CLASS="dropDown"></SELECT></TD>
                    </TR>
                <TR>
                    <TD ALIGN="left" class="boldText">&nbsp;Country</TD>
                    <TD>&nbsp;<SELECT ID="country" NAME="country" CLASS="dropDown"></SELECT></TD>
                    <TD ALIGN="left" class="boldText">&nbsp;State</TD>
                    <TD>&nbsp;<SELECT ID="state" NAME="state" CLASS="dropDown"></SELECT></TD>
                </TR>
                <TR> 
                    <TD ALIGN="left" class="boldText">&nbsp;Keywords&nbsp;<span class="mendotory">*</span></TD>
                    <TD COLSPAN="3">&nbsp;<INPUT TYPE="text" maxlength="40" SIZE="40" tabindex="1" name="keywords" id="keywords" value="">&nbsp;&nbsp;<INPUT TYPE="button" CLASS="btn" VALUE="Search" ONCLICK="javascript:processBusinessSearch();"/>&nbsp;<a href='#'  onclick='javascript:clearBusinessSearch();' ><img src="images/clear.gif" border="0"></a>&nbsp;</TD>
                </TR>
            </TABLE>            
        </form>
+1  A: 

Something like this should do it:

$("#keywords").keypress(function(e) {
    // if 'enter' is pressed
    if(e.which == 13) {
        return false;
    }
});
karim79
Hello i don't want how to submit form, I facing problem when i press enter in "keywords" text field form is going to submit i dont want that. This problem is along with <FORM> tag.If i remove <form> tag it works fine
Yashwant Chavan
@Yashwant - I misunderstood the question. So just 'return false' instead. I've edited my answer.
karim79
I need to find out the problem why its happing rather than just a fix
Yashwant Chavan
@Yashwant - In your question you say "Form is going to submit when i press enter in Keyword text field. there no such enter event bind in java script.". I think this has led to a *lot* of confusion. Is that statement the *intention* or is it the *problem*?
karim79
This is the problem i want to resolve why my form/page is going to submit on enter event
Yashwant Chavan
@Yashwant - Because there is only one text input. If you add a hidded one, e.g. `<input type="submit" style="display:none">` that should suppress the undesired behaviour.
karim79
+1  A: 

Another case of the "single textbox submit" problem in IE, see this SO question: "IE needs 2 textboxes to submit a button?".

One workaround is to place another textbox in the form with a style of "display: none"; that way, the enter button won't trigger the submit event. Or, you can use some Javascript to trigger the submit event... it all depends on what you want to achieve.

tijmenvdk
As per your comment when add one more textfield in the form its work properly. FORM/PAGE is not submitted
Yashwant Chavan
A: 

Try using the onsubmit event on the form instead:

<form onSubmit="return processBusinessSearch()" ...
Kragen
A: 
$("#keywords").keypress(function(e){ 
  if (e.which == 13)
    $("#businessSearchForm").submit(); 
});
kb
+1  A: 

I'm not sure I've interpreted your question, but I believe what you're wanting is for your processBusinessSearch() function to be called when the form is submitted, regardless of whether the user pressed the submit button or pressed enter.

For this, take a look at the submit event - add a handler for this event which calls your function.

Paul Dixon
+1 this makes more sense than my answer, i think i got the question wrong.
kb
Code is working fine on click of Search Button. But my problem is form/page is going to submit when i press enter key in keywords textfiled. When i Remove <form> tag from my above code it works properly i.e. FORM/Page is not going to submit on enter key
Yashwant Chavan