【问题标题】:Ajax request not pulling up jQuery dialog windowAjax 请求未拉起 jQuery 对话框窗口
【发布时间】:2013-11-15 02:49:19
【问题描述】:

请查看此jsFiddle...

这是一个使用AJAX请求来拉起内容的jQuery UI对话框。我似乎无法弄清楚怎么了,但是除了空白对话框之外,什么都没有弹出。

HTML...

<div id="#griffin"></div>
<ul>
    <li>
        <a href="ajax/ajax-bj.html" class="griffin-style all-about-bj" id="all-about-bj"></a>
    </li>
</ul>

JavaScript...

$(function() {
    $("#griffin").dialog({
        autoOpen: true,
        modal: true,
        width: 950,
        height: 'auto',
        show: 'fade',
        hide: 'fade',
        position: {my: "center top", at:"center top", of: window },
        buttons: {
            "I have Read and Understand": function() {
                $(this).dialog("close");
            }
        }
    });
        $(".griffin-style").on("click", function(e) {
            e.preventDefault();
            $("#griffin").html("");
            $("#griffin").dialog("option", "title", "Loading...").dialog("open");
            $("#griffin").load(this.href, function() {
                $(this).dialog("option", "title", $(this).find("h1").text());
                $(this).find("h1").remove();
            });
        });
    });

想法?

【问题讨论】:

    标签: javascript jquery ajax jquery-dialog


    【解决方案1】:

    你必须给出buttons参数下的命令。

    http://jsfiddle.net/aEwUF/4/

      $(function() {
        $( "#griffin" ).dialog({
          resizable: false,
          height:150,
          modal: true,
          buttons: {
            "I have read and understand the terms": function() {
              $( this ).dialog( "close" );
              $("p").html("You have accepted the terms");
              //write ajax requests in here..
            },
            Cancel: function() {
              $( this ).dialog( "close" );
            }
          }
        });
      });
    

    【讨论】:

    • 但是,我应该在哪里放置 href?我正在尝试在根目录上调用另一个页面。
    • 我需要它的样式与我原来的样式相同。这是它所在的实际页面。我把ajax请求放在你说的地方,但它仍然没有出现。该页面是here。想法?
    【解决方案2】:

    你需要添加一个jQuery UI对话框打开功能

    http://api.jqueryui.com/dialog/#method-open

    你必须在本地或在同一台服务器上运行它,因为 jsfiddle 由于同源策略而无法提取你的外部文件

    http://jsfiddle.net/aEwUF/7/

    $(function() {
        $("#griffin").dialog({
            autoOpen: true,
            modal: true,
            width: 950,
            height: 'auto',
            show: 'fade',
            hide: 'fade',
            position: {my: "center top", at:"center top", of: window },
            buttons: {
                "I have Read and Understand": function() {
                    $(this).dialog("close");
                }
            },
                        // add this
            open:function(event,ui){
                    $("#griffin").html("");
                    $("#griffin").load($(".griffin-style").attr("href"), function() {
                    $("#griffin").dialog("option", "title", $(this).find("h1").text());
                    $(".griffin-style").find("h1").remove();
                }); 
            }
        });
    
    
         $(".griffin-style").on("click", function(e) {
                    e.preventDefault();
                    $("#griffin").html("");
                    $("#griffin").dialog("option", "title", "Loading...").dialog("open");
                    $("#griffin").load(this.href, function() {
                        $("#griffin").dialog("option", "title", $(this).find("h1").text());
                        $(this).find("h1").remove();
                    });
        }); 
    
     });
    

    【讨论】:

    • 在我的页面中,它被拉为"ajax/ajax-request.html"。作为一个例子,我在小提琴中这样做了。
    • 您是否有更多来自上方的 HTML ......比如 #griffin 在哪里?
    • 对此感到抱歉。当我提出这个问题时,那部分并没有出现在代码中。我已经在上面编辑过。
    • 对这个乔纳森的想法?
    • @webfrogs 刚刚更新了您在 jQuery ui 对话框中使用 open 方法的答案 - api.jqueryui.com/dialog/#method-open -
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    相关资源
    最近更新 更多