Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys... im having problem with form submit in jquery. You see i have this form

<form id="myform" action="toMypage.php" method="POST">
  <input type="submit" value="reset" id="reset" />
  <input type="text" id="txt" />
</form>


as you can see in my form i have a button namely "reset" and using this structure this is my jquery code

C#
$("#form_search").submit(function(){

     alert("not reset");
     doFilterCall("search");
     return false;
		
	
});
$("#reset").click(function(){
     alert("reset");
     doFilterCall("reset");
     return false;	
});


but the problem here is that when i set my focus on my textfield and type something and hit the enter key after that it seems that the reset button is trigger instead of the $(form).submit();

how do we do a workaround on this? thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 21-Feb-12 0:58am    
First of all, please explain what do your want to achieve.
--SA
Madzmar25 21-Feb-12 1:10am    
i just want to reset all fields and session that i setup when i click on the reset button, but when i just hit enter while my cursor is on a textfield i want it to do a search filtering... but anyway i solve it... i just change the type of the reset button to "button" instead of "submit"
Sergey Alexandrovich Kryukov 21-Feb-12 1:27am    
I found your bugs and got a complete fix, please see my answer.
--SA
Sergey Alexandrovich Kryukov 21-Feb-12 1:33am    
Please accept my answer formally (green button); I gave you complete information, including the fix you mentioned in your post.
--SA

1 solution

There are two types of mistake in this code.

The first problem: you use different values of id in the form and the script. If you want to add an event to a form shown in the HTML code, use the id = "myform" in both places, that is, change first descriptor:

JavaScript
$("#myform").submit(function(){ /* ... */ }); //instead of "#form_search"


The second problem is: the each of the lines "return false;" blocks further event processing. Let's see what happens if you remove both return statements. First, the button click handle is executed; on the second step, the form handler submit is executed, and on the third step, the post goes.

If you use your return statement in the submit handler, the post execution is blocked, you will have only the click handler followed by the submit handler. If you use this return statement in the click handler, it will be executed, but the submit handler and post will be blocked.

[EDIT]

Probably you still need to use the return statement, but conditionally; for example, depending on the filtering results or any other conditions, such as the result of data validation.

[END EDIT]

I tested this code before posting this answer: changing the id value in the selector and commenting out of the return statements makes both handlers and the post executing.

—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900