Javascript document.form.submit 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5309107/
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
Javascript document.form.submit not working
提问by mic
I have created a form using JavaScript and tried to submit it but it show error message document.payment is null. plz help me
我使用 JavaScript 创建了一个表单并尝试提交它,但它显示错误消息 document.payment is null。请帮助我
var output_data = '<form id ="payment" name="payment" method="POST" action="https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/922635804601464" accept-charset="utf-8">'
output_data += '<input type="hidden" name="item_name_1" value="Peanut Butter">';
output_data +='<input type="hidden" name="item_description_1" value="Chunky peanut butter.">';
output_data += '<input type="hidden" name="item_quantity_1" value="1">';
output_data += '<input type="hidden" name="item_price_1" value="3.99">';
output_data += '<input type="hidden" name="item_currency_1" value="USD">';
output_data += '<input type="hidden" name="ship_method_name_1" value="UPS Ground">';
output_data += '<input type="hidden" name="ship_method_price_1" value="10.99">';
output_data += '<input type="hidden" name="ship_method_currency_1" value="USD">';
output_data += '<input type="hidden" name="tax_rate" value="0.0875">';
output_data += '<input type="hidden" name="tax_us_state" value="NY">';
output_data += '<input type="hidden" name="_charset_">';
output_data += '</form>';
//alert(output_data);
//return false;
output_data += "<script>";
output_data += "document.getElementById('payment').submit();";
output_data += "</script>";
document.write(output_data);
回答by Shadow Wizard is Ear For You
The form still not part of the document when you try to submit it. Change the line to:
当您尝试提交时,该表单仍然不是文档的一部分。将行更改为:
output_data += "window.onload = function() { document.getElementById('payment').submit(); }; ";
Edit: another "dirty" option is using timer:
编辑:另一个“脏”选项是使用计时器:
output_data += "window.setTimeout(function() { document.getElementById('payment').submit(); }, 500);";`
This will try to submit in half second delay.
这将尝试在半秒延迟后提交。
Edit 2: Just now looked deeper - You're doing it in the wrong way.
You need to use AJAX to perform the Checkout, using jQuery
AJAX becomes really simple to use.
Give this a try and let us know if you bump into walls. :)
编辑 2:刚才看得更深了 - 你做错了。
您需要使用 AJAX 来执行 Checkout,使用jQuery
AJAX 变得非常简单易用。
试试这个,如果你撞到墙壁,请告诉我们。:)
I'm almost sure that Google expose jsonp
service that allow cross domain AJAX, if not then you'll have to do it from server side code.
我几乎可以肯定 Google 公开jsonp
了允许跨域 AJAX 的服务,如果没有,那么您将不得不从服务器端代码中执行此操作。
回答by sv_in
Try this:
试试这个:
var output_data = '<form id ="payment" name="payment" method="POST" action="https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/922635804601464" accept-charset="utf-8">'
output_data += '<input type="hidden" name="item_name_1" value="Peanut Butter">';
output_data +='<input type="hidden" name="item_description_1" value="Chunky peanut butter.">';
output_data += '<input type="hidden" name="item_quantity_1" value="1">';
output_data += '<input type="hidden" name="item_price_1" value="3.99">';
output_data += '<input type="hidden" name="item_currency_1" value="USD">';
output_data += '<input type="hidden" name="ship_method_name_1" value="UPS Ground">';
output_data += '<input type="hidden" name="ship_method_price_1" value="10.99">';
output_data += '<input type="hidden" name="ship_method_currency_1" value="USD">';
output_data += '<input type="hidden" name="tax_rate" value="0.0875">';
output_data += '<input type="hidden" name="tax_us_state" value="NY">';
output_data += '<input type="hidden" name="_charset_">';
output_data += '</form>';
document.write(output_data);
document.getElementById('payment').submit();
回答by pg20
For those who come to this page looking for solutions of their problem, the problem is document.write. You are accessing a form created using document write and at the time is not available. (I have seen the problem more in https (not in http) and IE11.
对于来到此页面寻找问题解决方案的人来说,问题是 document.write。您正在访问使用文档写入创建的表单,但当时不可用。(我在 https(不是 http)和 IE11 中看到了更多的问题。
Following JavaScript solution works for me. Create a form using createElement
以下 JavaScript 解决方案对我有用。使用 createElement 创建表单
var oForm = document.createElement("FORM");
oForm.id = "taskslabelForm";
oForm.name = "taskslabelForm";
oForm.style.display = "none"; //You might not want to show the form
oForm.target="_self"; //Your target
oForm.method="POST";
oForm.action="YOUR-ACTION-HERE";
document.body.appendChild(oForm);
oForm.submit();
document.body.removeChild(oForm);
回答by Martin Jespersen
change
改变
output_data += "document.payment.submit();";
to
到
output_data += "document.getElementById('payment').submit();";
then it should work for you.
那么它应该适合你。
回答by CloudyMarble
id ="payment" name="payment"
You can refer to this by
你可以参考这个
getElementById()
getElementsByName()
If it doesnt work maby you have more Elements in your code with the same name as:
如果它不起作用,您的代码中有更多与以下名称相同的元素:
getElementById Returns the Element whose ID is given by elementId. If no such element exists, returns null. Behavior is not defined if more than one element has this ID.
getElementById 返回 ID 由 elementId 指定的 Element。如果不存在这样的元素,则返回 null。如果多个元素具有此 ID,则行为未定义。