在对话框中显示值

时间:2014-09-29 16:45:13

标签: javascript jquery html jsp

我正在动态创建一个对话框。我想在对话框中打印我从弹簧控制器收到的动态值。 由于$(document).ready(function()首先加载,我无法在对话框中显示动态值。

以下是我尝试的代码:

    var $dialog; 
var dynamicValue;
    var contextPath = "<%=request.getContextPath()%>";
    $(document).ready(function () {
        $dialog = $('<div></div>')
           .html('<table><tr><td>' + dynamicValue + '</td></tr></table>')
          .dialog({
            autoOpen: false,
            width:"400",
            height:300,
            modal: true,
            buttons: {
                "Close": function() {
                    $(this).dialog("close");
                }
            }
        });
    });


    function showDialog()
    {
        var xmlHttp;  
        if (window.XMLHttpRequest)
        {
            xmlHttp= new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
            xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url = contextPath+"/aboutATM.htm";
        xmlHttp.onreadystatechange = function() {
            handleServerResponse(xmlHttp);
        };
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);

        function handleServerResponse(xmlHttp)
        {   
           if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
           {
             $dialog.dialog('open');
             $dialog.dialog("option", "title", "Loading....").dialog("open"); 
          dynamicValue = xmlHttp.responseText;
           }   
        }
    }

1 个答案:

答案 0 :(得分:1)

修改了一下。它会对你有用..

 var $dialog; 
 var dynamicValue;
    var contextPath = "<%=request.getContextPath()%>";
    $(document).ready(function () {
        $dialog = $('<div></div>')
          .dialog({
            autoOpen: false,
            width:"400",
            height:300,
            modal: true,
            buttons: {
                "Close": function() {
                    $(this).dialog("close");
                }
            }
        });
    });


    function showDialog()
    {
        var xmlHttp;  
        if (window.XMLHttpRequest)
        {
            xmlHttp= new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
            xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url = contextPath+"/aboutATM.htm";
        xmlHttp.onreadystatechange = function() {
            handleServerResponse(xmlHttp);
        };
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);

        function handleServerResponse(xmlHttp)
        {   
           if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
           {
             $dialog.dialog('open');
             $dialog.dialog("option", "title", "Loading....").dialog("open"); 
             $dialog.html('<table><tr><td>' + xmlHttp.responseText + '</td></tr></table>')
           }   
        }
    }