【发布时间】:2018-09-21 12:27:39
【问题描述】:
我有一个包含 iFrame 的网页。此网页会自动向第三方 url 提交表单,该 url 会在 iFrame 中加载内容。我目前正在通过 jQuery 在 document.ready() 上自动提交表单,看起来 chrome 没有间歇性地提交此表单。
以下是我网页中的示例 html。
<form id="form_init" target="myFrame" action="https://somethirdpartyurl.com/req.jsp?queryParam=value" method="post" class=" edit accessAid">
<input name="_cs" id="_cs2" value="blah" type="hidden">
<input name="cs" id="cs2" value="blah" type="hidden">
<input type="hidden" name="howdy" value="oneMoreBlah">
<input type="hidden" name="myUrl" value="http://localhost:8000/myapp?_eventId=processing&sessionId=123">
<input type="hidden" name="MD" value="1234567891234567">
<div id="submit_button">
<p>Attention: This form can't be displayed because JavaScript is disabled in your browser.</p>
<p class="buttons">
<span class="buttonAsLink">
<input type="submit" name="" class="button autoSubmit" value="Click here to continue">
</span>
</p>
</div>
</form>
<iframe name="myFrame" id="myFrame" width="625px" height="480px" frameborder="0" marginheight="0" scrolling="auto" src="https://www.mywebsite.com/grey_spacer.gif"></iframe>
<script src="http://www.localhost:8000/myapp/webstatic/js/loadFrame.js" type="text/javascript"></script>
下面是 loadFrame.js 文件的 sn-p。
$(document).ready(function() {
$("#submit_button").length && ($("#submit_button").hide(), $("#submit_button input").hasClass("autoSubmit") && $("#submit_button input")[0].form.submit());
});
注意$("submit_button input")[0].form.submit()命令之前的所有条件都是成功的,我已经通过控制台日志确认了。
我现在面临的问题是,chrome 没有间歇性地提交上述表单。此问题在 FireFox 和 Safari 等其他浏览器中不存在。
一个观察结果是,当我打开开发者工具并检查禁用缓存选项时,大多数时候表单都没有被提交。如果我取消选中该选项,则主要是表单正在提交。 (大多数时候都是这样,不是全部)
还有一件事是,当我在控制台中手动执行相同的命令$("submit_button input")[0].form.submit() 时,chrome 正在提交表单并且一切正常。
有人能告诉我为什么会出现这个问题,或者至少是这个问题的替代解决方案吗?我希望提供所有必需的信息。如果您需要任何其他详细信息,请告诉我。
【问题讨论】:
-
好吧,
console.log()返回undefined所以这样的条件永远不会提交表单,但我假设您只是测试天气条件成功。无论如何,每当表单未提交时,您是否会在 devtools 的网络面板中看到错误消息? -
感谢您的指出。对,是真的。我添加以查看初始条件是否造成任何麻烦。即使没有
console.log(),我也面临同样的问题。不,我在 devtools 的网络/控制台选项卡中没有看到任何错误或警告消息。 -
您知道您的代码已损坏。仔细查看括号。最好将代码拆分为多行。
-
@SalmanA 我认为代码没有损坏。正如我所提到的,这是非常间歇性的情况。这段代码多年来一直有效,我最近遇到了这个问题。
-
嗯,有一个不匹配的括号,每个人都认为是问题所在。
标签: javascript jquery html forms google-chrome