javascript 单击按钮时更改表单操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14644133/
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
change form action on button click
提问by moritzw
Possible Duplicate:
How can I invoke multiple actions from a single form?
可能的重复:
如何从单个表单调用多个操作?
i have two buttons in a form. The one is for calculate, the other for send the calculated data in an email.
So i′ve made "action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"to calculate the whole thing in the form. Now i want to make the other button change the action to "mail.php" so i′ve made this:
我在一个表单中有两个按钮。一个用于计算,另一个用于通过电子邮件发送计算出的数据。所以我已经"action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"在表格中计算了整个事情。现在我想让另一个按钮将动作更改为“mail.php”,所以我做了这个:
<input type="submit" name="senden" value="Senden" onClick="document.form.action.value="mailer.php"">
<input type="submit" name="senden" value="Senden" onClick="document.form.action.value="mailer.php"">
But we all know, it doesn't work. So how do I get this to work?
但我们都知道,这是行不通的。那么我该如何让它发挥作用呢?
回答by amicngh
Do something like this.
做这样的事情。
<input type="button" name="senden1" value="Send" onClick="sendMailer()">
<input type="button" name="senden2" value="Calculate" onClick="caclculate()">
Then in calculatefunction just calculate the data and when user click Sendjust set the action and submit it in sendMailer().DON'T submit in calculate().
然后在calculate函数中只是计算数据,当用户点击时,Send只需设置动作并在sendMailer().DON'T submit 中提交。
document.<Form_Name>.action = 'mailer.php';
document.<Form_Name>.submit();
回答by Dilip Godhani
<form action="xxx.php" name="frm1" id="frm1">
<input type="button" name="senden1" value="Send" onClick="sendMailer()" id="senden1">
</from>
<form action="yyy.php" name="frm2" id="frm2">
<input type="button" name="senden2" value="Calculate" onClick="caclculate()">
</from>
<script>
$(document).ready(function($){
var form1 = $("#frm1");
var form2 = $("#frm2");
$("#senden1").click(function(){
form1.submit();
});
$("#senden2").click(function(){
form2.submit();
});
});
</script>

