Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to redirect a form to different URLs based on option selection. However, it will redirect the user to the URL without the click submit button.

then, I do some changes, now it redirects to bad path

How can I fix this?


What I have tried:

https://codepen.io/wanted88/pen/YzyRXaa[^]
Posted
Updated 24-Aug-21 20:23pm
Comments
ZurdoDev 18-May-20 8:55am    
What's wrong with the code you have?

The problem with your code is that the form continues to submit after you click the submit button so it goes to your "action". To stop that use preventDefault

JavaScript
$(document).ready(function(){
	$("form").submit(function(e){
    e.preventDefault();
		location.href = $('#selectbox').val();
	});
});


Note it still won't work in that tool as some of those sites will refuse to load in an iframe, so to break out of the iframe you will need

JavaScript
$(document).ready(function(){
	$("form").submit(function(e){
    e.preventDefault();
		window.top.location.href = $('#selectbox').val();
	});
});
 
Share this answer
 
$(document).ready(function(){
$("form").submit(function(e){
e.preventDefault();
window.top.location.href = $('#selectbox').val();
});
});

How to add target _blank code in there script ?
 
Share this answer
 
Comments
Richard Deeming 25-Aug-21 3:57am    
If you have a question, then ASK A QUESTION[^].

Your question is not a "solution" to someone else's question.

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