【问题标题】:Javascript submit() doesn't trigger form submissionJavascript submit() 不会触发表单提交
【发布时间】:2012-03-12 10:05:19
【问题描述】:

这是无效的代码:

<form id="paypal" name="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_parent" />Official website!
    <input type="hidden" name="item_name" value="Donation">
    <input type="hidden" name="cmd" value="_donations">
    <input type="hidden" name="bn" value="PP-DonationsBF">
    <input type="hidden" name="currency_code" id="currency_code" value="GBP" >
    <input type="hidden" name="business" id="business" value="paypalemail@null.com" >

    <a href="javascript: donate(paypal);" class="button1"><span></span><strong>£<input name="amount" type="text" id="ppAmount" style="text-align:center;" value="5" size="2" /> Donate!</strong></a>

    <input type="submit">
</form>

<div id="formtesting"></div>

<script type="text/javascript">

function donate(paypal) {
    document.getElementById("formtesting").innerHTML="maybe...";
    document.forms["paypal"].action = "https://www.paypal.com/cgi-bin/webscr";
    document.forms["paypal"].submit();
    document.getElementById("formtesting").innerHTML="did it work?";
}

</script>

我希望它在单击“button1”时提交,使用“javascript: donate(paypal)”提交按钮工作正常.. (它在表单测试 div 中打印“也许”,但不是“它有效吗?”:/)

【问题讨论】:

  • 据我所知,我认为 submit() 之后的任何代码都不会执行。如果我错了,请纠正我
  • 是错字还是你的&lt;form&gt;标签真的在这段代码sn-p的第一行关闭了?

标签: javascript forms


【解决方案1】:

我遇到了myForm.submit() 没有提交表单的问题。原来我的问题是我的提交按钮的id 是“提交”。一旦我将我的 id 更改为另一个名字 viola,它就起作用了。希望这可以帮助遇到此问题的相同变体的其他人。

编辑:还要记下 MalcomOcean 的评论,下面,name 标签也可能导致此问题

【讨论】:

  • 谢谢。虽然在我的情况下,它是提交按钮的name,而不是id
  • 你拯救了我的一天!
  • 如果您使用form.dispatchEvent( new Event( 'submit' ) ); 而不是form.submit() 名为“提交”的表单项将不再是问题。
【解决方案2】:

使用此代码它将起作用

function donate() {
document.getElementById("formtesting").innerHTML="maybe...";
document.paypal.action = "https://www.paypal.com/cgi-bin/webscr";
document.paypal.submit();
document.getElementById("formtesting").innerHTML="did it work?";
}

这里的 paypal 是表单的名称。

【讨论】:

  • 您使用的是哪个浏览器?
  • 您能否解释一下为什么document.paypal 有效而document["paypal"] 无效?
  • document.forms 是一个数组。所以 document.forms["paypal"] 没有意义,因为数组需要索引号,而不是字符串。
【解决方案3】:

面临同样的问题,从 javascript 提交没有提交表单。尝试了从 javascript 提交表单的所有方法,例如 document.formname.submit() 或 document.getElementById("formId").submit()。但这些都不起作用。

问题出在我的 html 页面中,有另一个按钮(不是我单击的那个),该按钮的名称是“提交”。当我将该名称更改为其他名称时,我的表单正在提交。

【讨论】:

    【解决方案4】:

    对我来说,这是由于试图访问不存在的元素造成的。即使在开发者控制台中查看,也不会出现错误日志提示,但代码会中止。我的代码:

    function MoveImageUp(id)
    {
        document.fms.aid.value= id;
        document.fms.mode.value='up'; // die! the form has no element called aid
    
        document.fms.submit();
    }
    

    仔细检查所有引用的元素。

    【讨论】:

      【解决方案5】:

      如果您使用的是 document.createElement("form"); 则删除所有“" 打开和关闭标签;

      【讨论】:

        【解决方案6】:

        这行得通:

        <script type="text/javascript">
        function donate(paypal) {
            document.getElementById("formtesting").innerHTML="maybe...";
            document.forms["paypal"].action = "https://www.paypal.com/cgi-bin/webscr";
            document.forms["paypal"].submit();
            document.getElementById("formtesting").innerHTML="did it work?";
        }
        </script>
        
        <form id="paypal" name="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_parent" />
            Official website!
            <input type="hidden" name="item_name" value="Donation">
            <input type="hidden" name="cmd" value="_donations">
            <input type="hidden" name="bn" value="PP-DonationsBF">
            <input type="hidden" name="currency_code" id="currency_code" value="GBP" >
            <input type="hidden" name="business" id="business" value="paypalemail@null.com" >
        
            <a href="javascript: donate(paypal); return false" class="button1">
                <strong>?<input name="amount" type="text" id="ppAmount" style="text-align:center;" value="5" size="2" /> Donate!</strong>
            </a>
            <input type="submit">
        </form>
        
        <div id="formtesting"></div>
        

        【讨论】:

        • 不知道这意味着什么......但上面的那个人修复了它(它是表单[“paypal”]位没有调用表单。)
        【解决方案7】:

        确保您使用的是 getElementById,而不是 getElementByid

        【讨论】:

        • 有时? JavaScript 什么时候不区分大小写?另外,这个答案似乎与问题无关。
        • 有什么区别?你已经写了两次同样的东西。
        猜你喜欢
        • 2013-07-19
        • 1970-01-01
        • 1970-01-01
        • 2014-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多