Solved

Javascript submit on enter error

Posted on 2010-09-23
7
307 Views
Last Modified: 2012-06-27
Ok...  Hopefully a simple fix...  I use this script to allow users to submit their search forms when they hit the enter key in the zip code field...  The problem I'm having is that if they have autocomplete on, and they type 43 and then the autocomplete shows 43123 under the field and the user clicks it and hits enter...  When it submits the search, it only registers the "43".  The autocomplete works fine without my script, but then it only submits if they hit submit...

Thanks...
onKeyPress="return submitenter(this,event)"





<SCRIPT TYPE="text/javascript">

<!--

function submitenter(myfield,e)

{

var keycode;

if (window.event) keycode = window.event.keyCode;

else if (e) keycode = e.which;

else return true;



if (keycode == 13)

   {

        formMyForm = document.getElementById("home_search_form");

        formMyForm.action = "vehicle_search.php";

        formMyForm.submit();

   }

else

   return true;

}

//-->

</SCRIPT>

Open in new window

0
Comment
Question by:stephenmp
  • 4
  • 2
7 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 33750155
add this on top, it maybe what you are looking for

if (myfield.length < 5) return false;

user will need to enter again to submit
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33750312
Try onkeyup instead onkeypress
0
 

Author Comment

by:stephenmp
ID: 33750321
That's more a validator... I can do that with jquery...  I'm looking to move the onkeypress to something that will only call on enter when the autocomplete enter has been pushed...
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 82

Expert Comment

by:leakim971
ID: 33750327
Else try a timer :


<SCRIPT TYPE="text/javascript">
<!--
	function submitenter(myfield,e) {
		var keycode = (window.event)?(window.event.keyCode):(e.which);
		if(keycode == 13) setTimeout("afterHalf()", 500);
		return true;
	}
	function afterHalf() {
		formMyForm = document.getElementById("home_search_form");
		formMyForm.action = "vehicle_search.php";
		formMyForm.submit();
	}
//-->
</SCRIPT>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33750519
Thanks for the points!
0
 

Author Comment

by:stephenmp
ID: 33750535
Thank you...
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33750536
You're very welcome!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now