javascript 表单提交在 chrome 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6754396/
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 form submit does not work in chrome
提问by Linto
I have the following html:
我有以下 html:
<body>
<form action="site1">
<input type="image" name="submit" border="0" src="images/front/search_travel.png" alt="" onclick="provider_popup();"/>
</form>
<form name="frmRight1" action="site2" target="_blank" >
<input type="hidden" name="sector_id" id="sector_id" value="90" />
<input type="submit" style="visibility:hidden;" />
</form>
<script type="text/javascript">
function provider_popup (){
document.frmRight1.submit();
return false;
}
</script>
</body>
And while I submit the button with name submit
, I get one another tab and it will load 'site2'
当我提交带有 name 的按钮时submit
,我会得到另一个选项卡,它将加载“site2”
But this process is not working in chrome.
但是这个过程在 chrome 中不起作用。
Let me know the reason
让我知道原因
采纳答案by James McCormack
Okay, you also needed to prevent the image button from submitting its form. Your function returned false, but you need to return that to the onclick, hence the problem. Do it like this:
好的,您还需要阻止图像按钮提交其表单。您的函数返回false,但您需要将其返回给onclick,因此出现问题。像这样做:
onclick="return provider_popup();"
onclick="return provider_popup();"
I recommend you ditch the <input type="image">
approach anyway, and use a pure element with an onclick
attribute. Makes your HTML much simpler i.e.
我建议您<input type="image">
无论如何都放弃这种方法,并使用带有onclick
属性的纯元素。使您的 HTML 更简单,即
<body>
<img src="images/front/search_travel.png" alt="Submit" onclick="provider_popup();" />
<form name="frmRight1" action="site2" target="_blank" >
<input type="hidden" name="sector_id" id="sector_id" value="90" />
<input type="submit" style="visibility:hidden;" />
</form>
<script type="text/javascript">
function provider_popup () {
document.forms['frmRight1'].submit();
}
</script>
</body>
Another alternative is to wrap the image in an anchor <a href...
and apply the onclick
to that.
另一种选择是将图像包裹在一个锚点中,<a href...
并将 应用onclick
到该锚点中。
回答by user2737761
I had the same problem. Took me all day. In my case the problem with Chrome was that Chrome was blocking my actions and stating that Chrome was blocking popups. Here is the code I used to solve this:
我有同样的问题。带了我一整天。就我而言,Chrome 的问题在于 Chrome 阻止了我的操作并说明 Chrome 阻止了弹出窗口。这是我用来解决这个问题的代码:
/** This is the script that will redraw current screen and submit to paypal. */
echo '<script>'."\n" ;
echo 'function serverNotifySelected()'."\n" ;
echo '{'."\n" ;
echo ' window.open(\'\', \'PayPalPayment\');'."\n" ;
echo ' document.forms[\'paypal_form\'].submit();'."\n" ;
echo ' document.forms[\'server_responder\'].submit();'."\n" ;
echo '}'."\n" ;
echo '</script>'."\n" ;
/** This form will be opened in a new window called PayPalPayment. */
echo '<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" name="paypal_form" method="post" target="PayPalPayment">'."\n" ;
echo '<input type="hidden" name="cmd" value="_s-xclick">'."\n" ;
echo '<input type="hidden" name="custom" value="'.$transaction_start.'">'."\n" ;
echo '<input type="hidden" name="hosted_button_id" value="'.$single_product->hosted_button_id.'">'."\n" ;
echo '<table>'."\n" ;
echo ' <tr>'."\n";
echo ' <td><input type="hidden" name="'.$single_product->hide_name_a.'" value="'.$single_product->hide_value_a.'">Local</td>'."\n" ;
echo ' </tr>'."\n" ;
echo ' <tr>'."\n" ;
echo ' <td>'."\n" ;
echo ' <input type="hidden" name="'.$single_product->hide_name_b.'" value="'.$single_product->hide_value_b.'" />'.$single_product->short_desc.' $'.$adj_price.' USD'."\n" ;
// <select name="os0">
// <option value="1 Day">1 Day .55 USD</option>
// <option value="All Day">All Day .50 USD</option>
// <option value="3 Day">3 Day .00 USD</option>
// <option value="31 Day">31 Day 7.00 USD</option>
// </select>
echo ' </td>'."\n" ;
echo ' </tr>'."\n" ;
echo '</table>'."\n" ;
echo '<input type="hidden" name="currency_code" value="USD">'."\n" ;
echo '</form>'."\n" ;
/** This form will redraw the current page for approval. */
echo '<form action="ProductApprove.php" name="server_responder" method="post" target="_top">'."\n" ;
echo '<input type="hidden" name="trans" value="'.$transaction_start.'">'."\n" ;
echo '<input type="hidden" name="prod_id" value="'.$this->product_id.'">'."\n" ;
echo '</form>'."\n" ;
/** No form here just an input and a button. onClick will handle all the forms */
echo '<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" alt="PayPal - The safer, easier way to pay online!" onclick="serverNotifySelected()">'."\n" ;
echo '<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">'."\n" ;
This code does not appear as a popup to Chrome. It is the code for just one button. When the button is pushed, it changes the current screen to pre-approval AND opens a new page for PayPal to fill.
此代码不会显示为 Chrome 的弹出窗口。它只是一个按钮的代码。当按下按钮时,它会将当前屏幕更改为预先批准并打开一个新页面供 PayPal 填写。
What I discovered is that if the new window opens WITH a button push, then Chrome does not treat it as a popup. If you try to open that new window with a timer, then Chrome treats the timed window opening as a popup.
我发现如果新窗口通过按下按钮打开,那么 Chrome 不会将其视为弹出窗口。如果您尝试使用计时器打开该新窗口,Chrome 会将定时窗口打开视为弹出窗口。
回答by James McCormack
Okay, your need to submit both forms simultaneously wasn't clear. This was tricky. The only way I found to do it was to introduce a small wait time:
好的,您不清楚需要同时提交两个表单。这很棘手。我发现这样做的唯一方法是引入一个小的等待时间:
<script type="text/javascript">
function provider_popup (){
document.forms['frmRight1'].submit();
setTimeout("doPost()", 10);
return false;
}
function doPost()
{
document.forms[0].submit();
}
</script>
回答by James McCormack
document.frmRight1
would only work in some browsers.
document.frmRight1
只能在某些浏览器中工作。
try
尝试
document.forms['frmRight1']
instead.
document.forms['frmRight1']
反而。
Alternatively give the form an id
like <form id="frmRight1" name...
and refer to it via document.getElementById('frmRight1')
.
或者给表格点id
赞<form id="frmRight1" name...
并通过 引用它document.getElementById('frmRight1')
。
See this for more info about properties of the document
: http://www.w3schools.com/jsref/dom_obj_document.asp
有关以下属性的更多信息,请参阅document
:http: //www.w3schools.com/jsref/dom_obj_document.asp
回答by AlienWebguy
Replace
代替
document.frmRight1.submit();
with
和
document.forms["frmRight1"].submit();