【问题标题】:form submitted in iframes failing on some computers在 iframe 中提交的表单在某些计算机上失败
【发布时间】:2014-10-30 09:45:24
【问题描述】:

使用函数在单独的 iframe 中提交 2 个表单,然后在父页面上提交表单。 (奇怪的问题是)我的方法似乎在其他计算机上的一台计算机上工作正常,它只在父页面上提交表单。浏览器是相同的(firefox,相同的版本)。 我想知道为什么会发生这种情况以及如何解决这个问题。这是html和函数

   <form id="projects" method="post">
    ...
    <input type="submit" class="button-primary" value="" >
    </form>
    <iframe id="lean">...<form method="post"><input type="submit" class="button-primary" value="submit" ></form></iframe>
    <iframe id="lean2">...<form method="post"><input type="submit" class="button-primary" value="submit" ></form></iframe>
    <button if="herewego">Submit all</button>

js

$(window).load(function(){
$('#herewego').click(function(){
if ($("#lean").contents().find("#_dtm_first_name").val().length >= 1){
$("#lean").contents().find('.button-primary').click();}
if ($("#lean2").contents().find("#_dtm_first_name").val().length >= 1){
$("#lean2").contents().find('.button-primary').click();}
$('#projects').find('.button-primary').click();
});

父页面中的表单重定向。 我的理论是,在过载的浏览器上,有足够的时间在 iframe 内提交表单,而在负载较少的浏览器上,它发生得如此之快,只有父级中的表单被提交。是这样吗?如果是这样,我应该如何重组我的 js,以便在父页面表单之前提交两个 iframe 表单?

【问题讨论】:

    标签: javascript jquery forms iframe


    【解决方案1】:

    Javascript 是异步的,所以如果你想先提交 iframe 表单,你需要依赖回调函数。有趣的是,JQuery 的 click() 函数似乎没有回调机制,因此您需要找到一些其他“标志”作为参考,例如,使用 iframe 表单提交事件怎么样?您可以确保仅在调用其他两个 onSubmit() 函数后才提交父表单(您可以使用计数器或其他东西..)。

    解决此问题的另一种可能是过度设计的方法是使用 JQuery 的$.when,我以前使用过它来保证涉及 Ajax 调用的同步事件,但不能 100% 确定这是否适用于您的情况,但这是一种很酷的技术:)

    var deferreds = getSomeDeferredStuff();
    
            $.when.apply($,deferreds).done(
                function() {
                    //Only execute this block after the iframe forms are submitted
                    console.log('Success!');
    
                    submitParentForm();
    
                }).fail(function() {
                    // Oh noes, something went wrong :(
                    console.log('The process failed');          
                }
            );
    
    function getSomeDeferredStuff() {
        var deferreds = [];
    
        deferreds.push(submitIframeForm1());
        deferreds.push(submitIframeForm2());
    
        return deferreds;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多