根据不同的提交按钮不同的表单动作

Ire*_*ing 2 javascript forms

如果我有一个有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>
Run Code Online (Sandbox Code Playgroud)

在某处<body>:

  <div style="visibility:hidden">
  <iframe NAME="iframe1" WIDTH="40" HEIGHT="40"></iframe>
  <iframe NAME="iframe2" WIDTH="40" HEIGHT="40"></iframe>
  </div>
Run Code Online (Sandbox Code Playgroud)

并以形式:

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

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

谢谢.

mpl*_*jan 5

你有两个问题

无论是

将按钮更改为type ="button"

要么

从功能中删除提交

Plain 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>
Run Code Online (Sandbox Code Playgroud)

不引人注目(推荐):

<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>
Run Code Online (Sandbox Code Playgroud)