将表单提交到2个不同的操作页面

时间:2010-03-15 09:50:55

标签: javascript html

我想要一个可以根据选择框提交到两个不同操作页面的表单。

如果我在选择框Products中选择,则操作将为products.html,如果我选择Users,则操作将为users.html

我发现很多例子都有两个提交按钮,但我只需要一个提交按钮。

关于如何做的任何想法?

4 个答案:

答案 0 :(得分:5)

你可以使用jQuery ......

如果选择的值等于某事

将表单属性操作设置为另一个非初始操作

这当然是伪代码..

这是一个有效的解决方案:

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#selectList").change(function(){
        if($('#selectList').val() == 1){
        $("#yourform").attr("action","secondaryaction");
        }
    });
});
</script>
</head>
<body>
<select name="dummy" id="selectList">
<option value="0">foo</option>
<option value="1">bar</option>
</select>

<form id="yourform" action="primaryaction">
bla bla
</form>
</body>
</html>

答案 1 :(得分:1)

尝试这样的事情(假设一个名为“myForm”的表格):

document.myForm.onsubmit = function() {
    if (this.mySelector.value == 'products') {
        this.action = 'products.html';
    }
    // the "else" isn't necessary: leave it at "users.html" as a default.
};

答案 2 :(得分:0)

了解新属性 可以在提交按钮上指定的表单提交的属性。属性包括:formaction,formenctype,formmethod,formnovalidate和formtarget

适用于IE&gt; = 11

我的例子将被证明点:

<form action="1.php">
  <input type="text" >
  <button value="go"> GOOOOOOOOOOOO</button>
 <button value="go" formaction="2.php"> DONNNNNNNNNNNNNNNNOOOO</button>
</form>

答案 3 :(得分:-1)

if(condition==products)
document.forms[0].action = 'products.html;
else
document.forms[0].action = 'users.html;
相关问题