javascript redirect based on option selected

六眼飞鱼酱① 提交于 2019-12-13 06:06:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!