问题
I need some help with the javascript redirect. I have the following code for the form and it is not working.
<form name="getstarted" width="500">
<p>
<select name="trade" id="trade">
<option selected="selected" value = "">Please Select License Trade</option>
<option value = "A">A-General Engineering</option>
<option value = "B">B-General Building</option>
<option value = "C2">C2 - Insulation and Acoustical</option>
</select>
</p>
<p>Do you currently hold an <strong>active</strong> contractors license in California? <br /> <br />
<INPUT TYPE="radio" id="questions" NAME="questions" VALUE="y">Yes
<INPUT TYPE="radio" id="questions" NAME="questions" VALUE="n">No</p>
<input type="button" value="Next" onClick = "direction()" />
</form>
<script language="javascript">
function direction(value)
{
if((trade == "A") && (questions == "y"))
{window.location = "http://localhost:8080/Math/page1.html";}
else{window.location = "http://localhost:8080/Math/page2.html";}
}
</script>
<script type="text/javascript">
$(document).ready(function(){
$(".icon a.tooltips").easyTooltip();
});
</script>
I am trying to have this code redirect users based on two different options selected.
Thank you
回答1:
Try this:
function direction(value) {
var trade = $("#trade").val();
var questions = $(".questions:checked").val();
if ((trade == "A") && (questions == "y")) {
window.location = "http://localhost:8080/Math/page1.html";
} else {
window.location = "http://localhost:8080/Math/page2.html";
}
}
Demo here
What I added:
var trade = $("#trade").val();
var questions = $(".questions:checked").val();
and removed the duplicate ID and used a class instead:
<INPUT TYPE="radio" class="questions" NAME="questions" VALUE="y"/>Yes
<INPUT TYPE="radio" class="questions" NAME="questions" VALUE="n"/>No
来源:https://stackoverflow.com/questions/19388382/javascript-redirect-based-on-option-selected