更改表单提交操作,然后提交表单

Rad*_*ish 4 html javascript forms jquery

我想添加一个按钮,以我的形式将基本上运行一些不同的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
Run Code Online (Sandbox Code Playgroud)

并为按钮:

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

用脚本:

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

小智 8

document.getElementById["adaugareanunt"]
Run Code Online (Sandbox Code Playgroud)

改成

document.getElementById("adaugareanunt")
Run Code Online (Sandbox Code Playgroud)


Rok*_*jan 7

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

不要将输入与name="submit"

.submit()如果您想使用该方法,还要确保您的表单中没有任何name="submit"输入。如果确实需要的话,可以用不同的方式称呼它,例如 iename="button_submit"

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

// 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. 
Run Code Online (Sandbox Code Playgroud)
<form id="form">
  <input type="submit" name="submit">
</form>
Run Code Online (Sandbox Code Playgroud)

  • 像这样修复它:`document.getElementById("submit").click();` (2认同)