【问题标题】:Need jQuery UI dialog "window" to scroll to the top when a form inside an iframe is submitted提交 iframe 内的表单时,需要 jQuery UI 对话框“窗口”滚动到顶部
【发布时间】:2011-11-23 16:26:15
【问题描述】:

我可能把事情复杂化了,但对整个过程相对较新,我不知道还能做什么。我在 iframe 中有一个订单表单,它位于 jQuery UI 对话框灯箱内。一切正常,除了一个问题:当表单提交并重定向到感谢页面或错误页面时,它会在底部滚动。

我尝试在感谢和错误页面的标签中使用onload="window.parent.scroll(0,0);",但没有任何作用。

也试过了:

<script type="text/javascript">
    $("#orderform").onsubmit(function() {
     $("#dialog").window.scrollTo(0,0);
     alert('triggered');
    });
</script>

我把脚本放在了我能想到的任何地方,索引页面、对话框内、iframe 内、表单页面中,甚至从未收到警报。

当前托管该站点的测试服务器是http://kinnill.com/dev/bluehydrangea/

【问题讨论】:

    标签: javascript jquery forms iframe


    【解决方案1】:

    不确定要滚动到顶部的页面。对话框,对话框所在的页面,还是 iframe 中的页面?

    查看您的代码后,我相信这就是您所追求的:

    如果对话框有一个在 iframe 更改后已经向下滚动的可滚动元素,那么您将希望定位任何具有“溢出:自动”的元素。如果该元素是#dialog,那么在表单所在的 iframe 页面上执行此操作(在您的情况下看起来是 form.php):

    <script type="text/javascript">
      $(function(){
        $("#orderform").submit(function() {//note: in jquery the onsubmit event is called "submit"
           $('#dialog', window.parent.document).scrollTop(0);//target #dialog from the parent window
           alert('triggered');
        });
      });
    </script>
    

    其他可能的解决方案供他人参考:

    如果是对话框所在的页面,则执行以下操作:

    如果表单在 iframe 中,则代码需要在 iframe 中。然后,您需要修改代码以定位父窗口。

    这应该是你要找的:

    <script type="text/javascript">
        $("#orderform").submit(function() {
         $(window.parent).scrollTop(0);
         alert('triggered');
        });
    </script>
    

    如果您要滚动的页面是感谢页面或错误页面,则在感谢和错误页面上添加以下内容:(感谢和错误页面上需要 jquery)

    <script type="text/javascript">
        $(function(){
           $(window).scrollTop(0);
        });
    </script>
    

    【讨论】:

    • 感谢页面和错误页面需要滚动,抱歉。尝试了代码,它触发了,但没有发生滚动。
    • K,我更新了我的答案。我查看了您的页面及其需要滚动的#dialog 元素。
    • 当我将脚本放在页面底部,在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    相关资源
    最近更新 更多