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

发布于 2024-10-31 20:54:37 字数 28 浏览 1 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

墨小墨 2024-11-07 20:54:37
<input type='submit' value='Submit' onclick='this.form.action="somethingelse";' />

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

 document.getElementById('form_id').action = 'somethingelse';
<input type='submit' value='Submit' onclick='this.form.action="somethingelse";' />

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

 document.getElementById('form_id').action = 'somethingelse';
笑着哭最痛 2024-11-07 20:54:37

如果您只需要支持现代浏览器,有一个简单的方法可以做到这一点:在提交按钮上,添加一个 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>

它也适用于 <按钮> 标签。

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

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

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:

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

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.

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

演多会厌 2024-11-07 20:54:37

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

像这样的事情:

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

请记住,onSubmit 的优先级高于操作属性。因此,每当您指定 onSubmit 值时,都会首先执行该操作,然后表单将转到操作。

You can also set onSubmit attribute's value in form tag. You can set its value using 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 onSubmit has higher priority than action attribute. So whenever you specify onSubmit value, that operation will be performed first and then the form will move to action.

一抹苦笑 2024-11-07 20:54:37

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

Attach to the submit button click event and change the action attribute in the event handler.

纸短情长 2024-11-07 20:54:37

您可以在 javascript 端执行此操作。

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

单击时,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;
}

You can do that on javascript side .

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

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

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;
}
寄人书 2024-11-07 20:54:37

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

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

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

<button onclick="this.form.action='/PropertiesList';" 
    Account Details </button>
短暂陪伴 2024-11-07 20:54:37

你可以试试这个:

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

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文