使用HTML下拉选择重定向

Redirect with HTML dropdown select

本文关键字:选择 重定向 HTML 使用      更新时间:2023-09-26

我有一个简单的HTML下拉选择框。

我希望这能起到"一旦选择了某个内容">页面重定向到自定义URL的作用

下面是我的标记

<fieldset id="size"><legend>Product Options</legend>
<div class="wpsc_variation_forms">
<table>
<tr><td class="col1"><label for="variation_select_48_5">Choose Size:</label></td>
<td class="col2"><select class="wpsc_select_variation" name="variation[5]" id="variation_select_48_5">
<option value="0" >-- Please Select --</option>
<option value="8" >Large</option>
<option value="7" >Medium</option>
<option value="6" >Small</option>
</select></td></tr>
</table>
</div><!--close wpsc_variation_forms-->
</fieldset>

我发现了一个类似的问题;但给出的解决方案似乎有点笨拙。

SELECT Dropdown在没有Javascript 的情况下重定向

使用jQuery:

$(function() {
    $('#variation_select_48_5').change(function() {
        document.location = 'http://customurl.abc';
    });
});

我所知道的最简单的方法是分配一个onchange事件来选择元素,并将选项的值设置为url。

<select onchange="window.location = this.options[this.selectedIndex].value;">
<option value="http://your-url">Large</option>
</select>

我会编写一个litte函数,然后根据返回的结果触发它

<?PHP
    function redirect($where){      
       header("Location: $where");
    }
    if ($_REQUEST['select1'] == '8'){
        redirect('http://example.com/somewhere.php');
    }elseif($_REQUEST['select1'] == '7'){
        redirect('http://example.com/elsewhere.php');
    }elseif($_REQUEST['select1'] == '6'){
        redirect('http://example.com/elsewhere.php');
    }
?>
<form>
<select name="select1" class="wpsc_select_variation" name="variation[5]" id="variation_select_48_5" onchange="this.form.submit()">
<option value="0" >-- Please Select --</option>
<option value="8" >Large</option>
<option value="7" >Medium</option>
<option value="6" >Small</option>
</select>

唯一需要的javascript是在select标记内触发提交onchange="this.form.submit()"

希望这能帮助

$("select#wpsc_select_variation").change(function(){
    if ($(this).val() !== 0) {
        switch($(this).val())
        { 
            case(8):
                window.location.href = http://www.8.com
            case(7):
                window.location.href = http://www.7.com
            case(6):
                window.location.href = http://www.6.com
        }
    }
});