根据所选的< option>动态更新表单动作. [英] Dynamically update form action based on selected <option>
问题描述
所以,这就是我想要做的..我想要一个HTML下拉列表,其提交按钮根据下拉列表的值而改变.
So, this is what I am trying to do.. I want a dropdown in HTML with a submit button that changes based on the value of the dropdown.
所以,当我有这个时:
<select name="sizes" id="select13">
<option value="All">All</option>
<option value="20x30">20x30</option>
<option value="30x30">30x30</option>
...
</select>
我需要一个检查值的按钮.如果value = 20x30,则使用网址www.example.com/20x30
What I need is a button that checks what the value is. If value = 20x30 then use URL www.example.com/20x30
如果值是30x30,则使用网址www.example.com/30x30
If value is 30x30 then use URL www.example.com/30x30
我距离PHP专家还很远,所以任何能够将我引向正确方向的人都可以挽救生命:)
I am far from a PHP expert, so anyone that is able to set me off in the right direction would be a life saver :)
推荐答案
您可以尝试以下方法:
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/(所选值).
When you click on the 'GO' button it redirects to 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代码如下:
Also, if you want to submit a form and then do the redirection. The PHP code would be as follows:
<?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');
}
这篇关于根据所选的< option>动态更新表单动作.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!