admin管理员组

文章数量:1336112

I would like to have a form that can submit to two different action pages based on a select box.

Meaning if I select in the select box Products, the action would be products.html, and if I select Users, the action would be users.html

I have found many examples that have two submit buttons, but I need only one submit button.

Any ideas on how to do it?

I would like to have a form that can submit to two different action pages based on a select box.

Meaning if I select in the select box Products, the action would be products.html, and if I select Users, the action would be users.html

I have found many examples that have two submit buttons, but I need only one submit button.

Any ideas on how to do it?

Share asked Mar 15, 2010 at 9:50 AdnanAdnan 26.4k18 gold badges83 silver badges111 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

you can use jQuery for that ...

if selected value equals something

set form attribute action to the other thing not initial action

this is pseudo code of course ..

here is a working solution :

<html>
<head>
<script type="text/javascript" src="http://code.jquery./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>

Try something like this (assuming a form named "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.
};

read about new attributes Attributes for form submission that may be specified on submit buttons. The attributes are: formaction, formenctype, formmethod, formnovalidate, and formtarget

Works in IE >= 11

my example for will be demonstrated point :

<form action="1.php">
  <input type="text" >
  <button value="go"> GOOOOOOOOOOOO</button>
 <button value="go" formaction="2.php"> DONNNNNNNNNNNNNNNNOOOO</button>
</form>
if(condition==products)
document.forms[0].action = 'products.html;
else
document.forms[0].action = 'users.html;

本文标签: javascriptSubmit form to 2 different action pageStack Overflow