如何通过 JavaScript 设置表单动作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2701041/
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
How to set form action through JavaScript?
提问by umar
I have an HTML form whose action should be set dynamically through JavaScript. How do I do it? Here is what I am trying to achieve:
我有一个 HTML 表单,它的操作应该通过 JavaScript 动态设置。我该怎么做?这是我想要实现的目标:
<script type="text/javascript">
function get_action() { // inside script tags
return form_action;
}
</script>
<form action=get_action()>
...
</form>
回答by BalusC
You cannot invoke JavaScript functions in standard HTML attributes other than onXXX. Just assign it during window onload.
您不能在标准 HTML 属性中调用 JavaScript 函数,而不是onXXX. 只需在窗口加载期间分配它。
<script type="text/javascript">
window.onload = function() {
document.myform.action = get_action();
}
function get_action() {
return form_action;
}
</script>
<form name="myform">
...
</form>
You see that I've given the form a name, so that it's easily accessible in document.
您会看到我已经给出了表单 a name,以便在document.
Alternatively, you can also do it during submitevent:
或者,您也可以在submit活动期间进行:
<script type="text/javascript">
function get_action(form) {
form.action = form_action;
}
</script>
<form onsubmit="get_action(this);">
...
</form>
回答by Rabbott
Plain JavaScript:
纯 JavaScript:
document.getElementById('form_id').action; //Will retrieve it
document.getElementById('form_id').action = "script.php"; //Will set it
Using jQuery...
使用jQuery...
$("#form_id").attr("action"); //Will retrieve it
$("#form_id").attr("action", "/script.php"); //Will set it
回答by Piyush
Very easy solution with jQuery:
使用 jQuery 非常简单的解决方案:
$('#myFormId').attr('action', 'myNewActionTarget.html');
Your form:
您的表格:
<form action=get_action() id="myFormId">
...
</form>
回答by Eugen Konkov
Actually when we want this we want to change action depending on which submit button we press.
Here you do not need even assign nameor idto the form. Just use formproperty of clicked element:
实际上,当我们想要这个时,我们想根据我们按下的提交按钮来改变动作。
在这里,您甚至不需要分配name或分配id给表单。只需使用form点击元素的属性:
<form action = "/default/page" >
<input type=submit onclick='this.form.action="/this/page";' value="Save">
<input type=submit onclick='this.form.action="/that/page";' value="Cancel">
</form>
回答by Diodeus - James MacFarlane
document.forms[0].action="http://..."
...assuming it is the first form on the page.
...假设它是页面上的第一个表单。
回答by svinto
Do as Rabbott says, or if you refuse jQuery:
按照 Rabbott 说的去做,或者如果你拒绝 jQuery:
<script type="text/javascript">
function get_action() { // inside script tags
return form_action;
}
</script>
<form action="" onsubmit="this.action=get_action();">
...
</form>
回答by TheLastCodeBender
Change the action URL of a form:
更改表单的操作 URL:
<form id="myForm" action="">
</form>
<button onclick="changeAction()">Try it</button>
<script>
function changeAction() {
document.getElementById("myForm").action = "url/action_page.php";
}
</script>
回答by Michael Stokes
Setting form action after selection of option using JavaScript
使用 JavaScript 选择选项后设置表单操作
<script>
function onSelectedOption(sel) {
if ((sel.selectedIndex) == 0) {
document.getElementById("edit").action =
"http://www.example.co.uk/index.php";
document.getElementById("edit").submit();
}
else
{
document.getElementById("edit").action =
"http://www.example.co.uk/different.php";
document.getElementById("edit").submit();
}
}
</script>
<form name="edit" id="edit" action="" method="GET">
<input type="hidden" name="id" value="{ID}" />
</form>
<select name="option" id="option" onchange="onSelectedOption(this);">
<option name="contactBuyer">Edit item</option>
<option name="relist">End listing</option>
</select>

