两个下拉条件有一些条件

时间:2018-05-17 13:34:13

标签: javascript

我有一个情况,我有两个下拉选择器,第二个选择器的选项取决于第一个选择器的选项。 因此,第二个下拉列表是最后一个,所以如果有人从第二个下拉列表中选择选项,它应该重定向到特定的URL。

排序工作是否有任何人可以帮助重定向,这是我的代码到目前为止

HTML

<div class="category_div" id="category_div">Please Select Weeks:
    <select name="category" class="required-entry" id="category" onchange="javascript: firstdropdown(this.options[this.selectedIndex].value);">
        <option value="">Select Weeks</option>
        <option value="Week 1">Week 1</option>
        <option value="Week 2">Week 2</option>
</select>
</div>
<div class="sub_category_div" id="sub_category_div">Please Select Days:
    <script type="text/javascript" language="JavaScript">
        document.write('<select name="subcategory" id="subcategory" ><option  value="">Select Days</option></select>');
    </script>
    <noscript>
        <select name="subcategory" id="subcategory"">
            <option value="">Select Days</option>
        </select>
    </noscript>
</div>

的Javascript

function firstdropdown(listindex)
{

    document.getElementById("subcategory").length = 0;
    switch (listindex)
    {
        case "Week 1" :
            document.getElementById("subcategory").options[0]=new Option("Please select days for week 1","");
            document.getElementById("subcategory").options[1]=new Option("Day 1","http://www.example.org");
            document.getElementById("subcategory").options[2]=new Option("Day 2","http://www.example.com");
            document.getElementById("subcategory").options[3]=new Option("Day 3","http://www.example.net");
            document.getElementById("subcategory").options[4]=new Option("Day 4","http://www.example.com");
            document.getElementById("subcategory").options[5]=new Option("Day 5","http://www.example.net");
            break;

        case "Week 2" :
            document.getElementById("subcategory").options[0]=new Option("Please select days for week 2","");
            document.getElementById("subcategory").options[1]=new Option("Day 1","http://www.example.org");
            document.getElementById("subcategory").options[2]=new Option("Day 2","http://www.example.com");
            document.getElementById("subcategory").options[3]=new Option("Day 3","http://www.example.net");
            document.getElementById("subcategory").options[4]=new Option("Day 4","http://www.example.com");
            document.getElementById("subcategory").options[5]=new Option("Day 5","http://www.example.net");
            break;
    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

我添加了包含事件的函数重定向;然后使用event.target.value获得所选opt的值。

要重定向我使用window.location.href 试试这个例子:

HTML:

<div class="category_div" id="category_div">Please Select Weeks:
    <select name="category" class="required-entry" id="category" onchange="javascript: firstdropdown(this.options[this.selectedIndex].value);">
        <option value="">Select Weeks</option>
        <option value="Week 1">Week 1</option>
        <option value="Week 2">Week 2</option>
</select>
</div>
<div class="sub_category_div" id="sub_category_div" onchange="redirect(event)">Please Select Days:
    <script type="text/javascript" language="JavaScript">
        document.write('<select name="subcategory" id="subcategory" ><option  value="">Select Days</option></select>');
    </script>
    <noscript>
        <select name="subcategory" id="subcategory">
            <option value="">Select Days</option>
        </select>
    </noscript>
</div>

js:

function firstdropdown(listindex)
{

    document.getElementById("subcategory").length = 0;
    switch (listindex)
    {
        case "Week 1" :
            document.getElementById("subcategory").options[0]=new Option("Please select days for week 1","");
            document.getElementById("subcategory").options[1]=new Option("Day 1","http://www.example.org");
            document.getElementById("subcategory").options[2]=new Option("Day 2","http://www.example.com");
            document.getElementById("subcategory").options[3]=new Option("Day 3","http://www.example.net");
            document.getElementById("subcategory").options[4]=new Option("Day 4","http://www.example.com");
            document.getElementById("subcategory").options[5]=new Option("Day 5","http://www.example.net");
            break;

        case "Week 2" :
            document.getElementById("subcategory").options[0]=new Option("Please select days for week 2","");
            document.getElementById("subcategory").options[1]=new Option("Day 1","http://www.example.org");
            document.getElementById("subcategory").options[2]=new Option("Day 2","http://www.example.com");
            document.getElementById("subcategory").options[3]=new Option("Day 3","http://www.example.net");
            document.getElementById("subcategory").options[4]=new Option("Day 4","http://www.example.com");
            document.getElementById("subcategory").options[5]=new Option("Day 5","http://www.example.net");
            break;
    }
    return true;
}
function redirect(event){
window.location.href = event.target.value;
}