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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19363857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:47:37  来源:igfitidea点击:

Change form submit action and then submit the form

javascriptjqueryhtmlforms

提问by Radish

I'm trying to add a button to my form that would essentially run some different php code than my regular form submission code, that would, instead of emailing me the form, convert my page into a nice pdf ready to print. I have everything working except that pressing the button is giving me an error.

我正在尝试向我的表单添加一个按钮,该按钮基本上会运行一些与我的常规表单提交代码不同的 php 代码,而不是通过电子邮件将表单发送给我,而是将我的页面转换为准备好打印的漂亮 pdf。我一切正常,只是按下按钮给我一个错误。

Firebug says : enter image description here

萤火虫 说: 在此处输入图片说明

Here's the code:

这是代码:

<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

and for the button:

和按钮:

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

with the script:

使用脚本:

<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>

采纳答案by Roko C. Buljan

Adaugareanuntis an object key:
getElementById["adaugareanunt"]

Adaugareanunt是一个对象键
getElementById["adaugareanunt"]

"Adaugareanunt"is a stringpassed to the getElementById()methodas argument:
getElementById('adaugareanunt')?

"Adaugareanunt"是一个字符串传递给getElementById()方法的参数:
getElementById('adaugareanunt')

回答by babonamu

document.getElementById["adaugareanunt"]

change to

改成

document.getElementById("adaugareanunt")

回答by Muhammad Faran Ali

This works fine

这工作正常

<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>

回答by 123

Change the block brackets : []

更改块括号:[]

To

The round brackets : ()

圆括号:()

Square brackets means new Array.

方括号表示新数组。

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