根据选定的<option> </option>动态更新表单操作

时间:2013-06-12 20:51:53

标签: javascript html

所以,这就是我要做的事情。我想要一个带有提交按钮的HTML下拉列表,该按钮会根据下拉列表的值进行更改。

所以,当我有这个:

<select name="sizes" id="select13">
<option value="All">All</option>
<option value="20x30">20x30</option>
<option value="30x30">30x30</option>
...
</select>

我需要的是一个检查值是什么的按钮。如果值= 20x30,则使用URL www.example.com/20x30

如果值为30x30,请使用网址www.example.com/30x30

我远非PHP专家,所以任何能够让我朝着正确的方向前进的人都将是一个救生员:)

3 个答案:

答案 0 :(得分:6)

一些简单的Javascript就足够了:

<form id="FORM_ID" action="DEFAULT_ACTION">
    <select name="sizes" id="select13">
        <option value="All">All</option>
        <option value="20x30">20x30</option>
        <option value="30x30">30x30</option>
    </select>
</form>

<script type="text/javascript">
document.getElementById('select13').onchange = function(){
    document.getElementById('FORM_ID').action = '/'+this.value;
}
</script>

答案 1 :(得分:1)

您的下拉列表需要onchange个活动:

document.getElementById("select13").onchange = function() {
    var currentVal = this.value;

    if (currentVal == "20x30") {
        //do stuff
    }
}

答案 2 :(得分:1)

你可以试试这个:

<强>使用Javascript:

function goto() {
    window.location = "http://www.example.com/"+document.getElementById('select13').value;
}

<强> HTML:

<select name="sizes" id="select13">
    <option value="All">All</option>
    <option value="20x30">20x30</option>
    <option value="30x30">30x30</option>
</select>
<button onclick='goto()'>Go</button>

当您点击“开始”按钮时,它会重定向到example.com/(the selected value)。

这是一个带有例子的JSFiddle


编辑以符合您的评论:

function goto() {
    var selection = document.getElementById('select13').value;
    if (selection != 'All') {
        //window.location = "http://www.example.com/"+selection;
        alert("http://www.example.com/" + selection);
    } else {
        alert("Error: You must pick something");
    }
}

此外,如果您想提交表单然后进行重定向。 PHP代码如下:

<?php
    //Process your form without echoing anything before the header function.

    if($_REQUEST['sizes'] != 'All'){
        header('location:http://example.com/'.$_REQUEST['sizes']);
    }
    else{
        header('location:http://example.com/form.php');
    }
相关问题