gpt4 book ai didi

javascript - 根据不同的提交按钮执行不同的表单操作

转载 作者:行者123 更新时间:2023-11-29 17:26:13 25 4
gpt4 key购买 nike

如果我有一个有 2 个按钮的表单,当我单击 Button1 时,它将是 action="submit1.php" , 如果 Button2 那么 action="submit2.php" .

我试过这个:

  <script language="javascript">

function Button1()
{
document.Form1.action = "submit1.php"
document.Form1.target = "iframe1";
document.Form1.submit();

}

function Button2()
{
document.Form1.action = "submit2.php" ;
document.Form1.target = "iframe2";
document.Form1.submit();
}

</script>

<body> 的某处:

  <div style="visibility:hidden">
<iframe NAME="iframe1" WIDTH="40" HEIGHT="40"></iframe>
<iframe NAME="iframe2" WIDTH="40" HEIGHT="40"></iframe>
</div>

形式为:

  <form name="Form1" id="Form1" method="post">
<input type="submit" name="Button1" onclick="Button1();">
<input type="submit" name="Button2" onclick="Button2();">
</form>

它不起作用,我做错了什么吗?

谢谢。

最佳答案

你有两个问题

要么

将按钮更改为 type="button"

从函数中删除提交

纯 JS(使用更简单的表单访问):

<script language="javascript">
function Button(theButton) {
var theForm = theButton.form;
if (theButton.name=="Button1") {
theForm.action = "submit1.php"
theForm.target = "iframe1";
}
else {
theForm.action = "submit2.php"
theForm.target = "iframe2";
}
}
</script>

<form method="post" action="nojsavailable.php">
<input type="submit" name="Button1" onclick="Button(this);" />
<input type="submit" name="Button2" onclick="Button(this);" />
</form>

不显眼(推荐):

<script language="javascript">
window.onload=function() {
var buttons = document.getElementsByName("button");
for (var i=0;i<buttons.length;i++) {
buttons[i].onclick=function() {
var idx = i+1;
var theForm = this.form;
theForm.action = "submit"+idx+".php"
theForm.target = "iframe"+idx;
}
}
}
</script>

<form method="post" action="nojsavailable.php">
<input type="submit" name="button" id="button1" />
<input type="submit" name="button" id="button2" />
</form>

关于javascript - 根据不同的提交按钮执行不同的表单操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8589575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com