Html 提交时设置表单的操作属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5640916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:56:13  来源:igfitidea点击:

Set a form's action attribute when submitting?

htmlformssubmitaction

提问by dave

How do I change a form's action attribute right after clicking the submit button?

单击提交按钮后,如何立即更改表单的操作属性?

采纳答案by Oded

Attach to the submit button clickevent and change the actionattribute in the event handler.

附加到提交按钮click事件并更改action事件处理程序中的属性。

回答by James

<input type='submit' value='Submit' onclick='this.form.action="somethingelse";' />

Or you can modify it from outside the form, with javascript the normal way:

或者您可以从表单外部修改它,使用 javascript 正常方式:

 document.getElementById('form_id').action = 'somethingelse';

回答by Nathan Friedly

There's a simple way to do this if you only need to support modern browsers: on your submit button, add a formaction="/alternate/submit/url"attribute like so:

如果您只需要支持现代浏览器,有一种简单的方法可以做到这一点:在您的提交按钮上,添加如下formaction="/alternate/submit/url"属性:

<form>
    [fields]
    <input type="submit" value="Submit to a" formaction="/submit/a">
    <input type="submit" value="submit to b" formaction="/submit/b">
</form>

It also works on <button>tags.

它也适用于<button>标签。

The gotcha is that old versions of IE (<10) and the Android Browser (<4.0) do not support it. So, if you need to support older browsers, then the existing JS answers will probably work better for you.

问题是旧版本的 IE (<10) 和 Android 浏览器 (<4.0) 不支持它。因此,如果您需要支持较旧的浏览器,那么现有的 JS 答案可能更适合您。

More info: http://www.wufoo.com/html5/attributes/13-formaction.html

更多信息:http: //www.wufoo.com/html5/attributes/13-formaction.html

回答by Bhavik Shah

You can also set onSubmitattribute's value in form tag. You can set its value using Javascript.

您还可以onSubmit在表单标签中设置属性的值。您可以使用 Javascript 设置其值。

Something like this:

像这样的东西:

<form id="whatever" name="whatever" onSubmit="return xyz();">
    Here is your entire form
    <input type="submit">
</form>;

<script type=text/javascript>
function xyz() {
  document.getElementById('whatever').action = 'whatever you want'
}
</script>

Remember that onSubmithas higher priority than action attribute. So whenever you specify onSubmitvalue, that operation will be performed first and then the form will move to action.

请记住,onSubmit它比 action 属性具有更高的优先级。因此,每当您指定onSubmit值时,将首先执行该操作,然后表单将移动到操作。

回答by Priyank

You can do that on javascript side .

你可以在 javascript 端做到这一点。

<input type="submit" value="Send It!" onClick="return ActionDeterminator();">

When clicked, the JavaScript function ActionDeterminator() determines the alternate action URL. Example code.

单击时,JavaScript 函数 ActionDeterminator() 确定备用操作 URL。示例代码。

function ActionDeterminator() {
  if(document.myform.reason[0].checked == true) {
    document.myform.action = 'http://google.com';
  }
  if(document.myform.reason[1].checked == true) {
    document.myform.action = 'http://microsoft.com';
    document.myform.method = 'get';
  }
  if(document.myform.reason[2].checked == true) {
    document.myform.action = 'http://yahoo.com';
  }
  return true;
}

回答by user2099484

HTML5's formactiondoes not work on old IE browsers. An easy fix, based on some of the responses above, is:

HTML5 的formaction不适用于旧的 IE 浏览器。根据上面的一些回复,一个简单的解决方法是:

<button onclick="this.form.action='/PropertiesList';" 
    Account Details </button>

回答by VaChinaka

You can try this:

你可以试试这个:

<form action="/home">
  
  <input type="submit" value="cancel">
  
  <input type="submit" value="login" formaction="/login">
  <input type="submit" value="signup" formaction="/signup">
  
</form>