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

新手上路,请多包涵

我正在尝试向我的表单添加一个按钮,该按钮本质上会运行一些与我的常规表单提交代码不同的 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>

原文由 Radish 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 229
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" 输入。如果确实需要,请以不同的方式调用它,例如 ie 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>

原文由 Roko C. Buljan 发布,翻译遵循 CC BY-SA 4.0 许可协议

document.getElementById["adaugareanunt"]

改成

document.getElementById("adaugareanunt")

原文由 babonamu 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题