最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Submit form to 2 different action page - Stack Overflow

programmeradmin1浏览0评论

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;
发布评论

评论列表(0)

  1. 暂无评论