开发者

Submit form to 2 different action page

开发者 https://www.devze.com 2022-12-23 02:44 出处:网络
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 Us

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?


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.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>


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消