Jquery - 在提交时动态构建表单操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2675580/
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
Jquery - Dynamically build form action on submit?
提问by Probocop
I'm trying to have the action of a HTML form built when the user clicks the submit button.
我正在尝试在用户单击提交按钮时构建 HTML 表单的操作。
So the user fills in a form, clicks submit, then the action gets built thenit actually gets submitted. The reason being is because the form has a load of options on it which will be passed to a script.
因此,用户填写表单,单击提交,然后构建操作,然后实际提交。原因是因为表单上有大量选项,这些选项将传递给脚本。
How would I go about doing this with Jquery?
我将如何使用 Jquery 执行此操作?
回答by Matt Ball
Untested, but this should at least get you on your way:
未经测试,但这至少应该让你上路:
$('#myForm').submit(function (event)
{
var action = '';
// compute action here...
$(this).attr('action', action);
});
回答by Jarek
回答by Guffa
The plain Javascript solution would have a function:
普通的 Javascript 解决方案将有一个功能:
function changeAction() {
this.action = 'the dynamic action';
return true;
}
In the form you would set the onsubmit event:
在表单中,您将设置 onsubmit 事件:
<form ... onsubmit="return changeAction();">
To do the same using jQuery would be:
使用 jQuery 做同样的事情是:
$(function(){
$('IdOfTheForm').submit(function(){
this.action = 'the dynamic action';
return true;
});
});