【问题标题】:Change form submit action and then submit the form更改表单提交操作,然后提交表单
【发布时间】:2013-10-14 15:34:48
【问题描述】:

我正在尝试在我的表单中添加一个按钮,该按钮将运行一些与我的常规表单提交代码不同的 php 代码,而不是通过电子邮件将表单发送给我,而是将我的页面转换为可以打印的漂亮 pdf。除了按下按钮给我一个错误之外,我一切正常。

萤火虫 说:

代码如下:

<form id="adaugareanunt" name="adaugareanunt" action="mailerPDF.php" method="post">
    <table width="535" border="0" cellspacing="2" cellpadding="3">
         <tr class="TrDark">
         //... more form code

对于按钮:

<div style="text-align:right"><img src="images/print-button.png" onClick="chgAction()" width="60px" height="20px"></div>

使用脚本:

<script language="JavaScript" type="text/JavaScript">
    function chgAction()
        {
            document.getElementById["adaugareanunt"].action = "mailerXFDF.php";
            document.getElementById["adaugareanunt"].submit();
            document.getElementById["adaugareanunt"].action = "mailerPDF.php";

        }
</script>

【问题讨论】:

    标签: javascript jquery html forms


    【解决方案1】:
    document.getElementById["adaugareanunt"]
    

    改成

    document.getElementById("adaugareanunt")
    

    【讨论】:

      【解决方案2】:
      const EL_form = document.getElementById("form"); 
      EL_form.action = "someOtherURL.php";
      EL_form.submit();
      // PS! Make sure you don't have any name="submit" inputs in your form
      

      不要将输入与name="submit" 一起使用

      如果您想使用.submit() 方法,请确保您的表单中没有任何name="submit" 输入。如果真的需要像name="button_submit"这样的不同的称呼。

      name="submit" 输入的问题是:它们超过了函数 submit,因为任何具有集合 name 属性的元素都会成为该表单元素的属性。
      问题示例:

      // EXAMPLE OF THE ISSUE:
      
      const EL_form = document.getElementById("form");
      
      // Why does EL_form.submit() not work?
      
      console.log(EL_form.submit);   // It's the actual INPUT with name="submit"
      console.log(EL_form.submit()); // Therefore the "Not a function" error. 
      <form id="form">
        <input type="submit" name="submit">
      </form>

      【讨论】:

      • 我仍然在提交行中收到错误消息。 'document.getElementById("adaugareaunt").submit();' 'TypeError: document.getElementById(...).submit 不是函数'
      • 这样修复它:document.getElementById("submit").click();
      【解决方案3】:

      这很好用

      <script language="javascript" type="text/javascript">
          function chgAction()
              {
                    document.getElementById("adaugareanunt").action ="mailerXFDF.php";
                    document.getElementById("adaugareanunt").submit();
                    document.getElementById("adaugareanunt").action ="mailerPDF.php";
      
              }    
      </script>
      

      【讨论】:

        【解决方案4】:

        更改块括号:[]

        圆括号:()

        方括号表示新数组。

        var ar = newArray( " a " , " b " ) ; 
        var ar = [ " a " , " b " ] ;
        

        【讨论】: