2011-03-26 152 views
0

的Xml显示jQuery的对话框

<?xml version="1.0" encoding="utf-8"?> 
<Questions> 
    <Question> 
    <Id>1</Id> 
    <Text>aaaa</Text>  
</Question> 
<Question> 
    <Id>2</Id> 
    <Text>bbb</Text>  
</Question> 
</Questions> 

HTML

<table dir="rtl" width="400px"> 
      <tr> 
       <td> 
        <span id="signuptitle">ques* : </span> 
       </td> 
       <td> 
        <select id="sctQuestion" name="D2"> 
         <option></option> 
        </select> 
       </td> 
      </tr> 

代码1

function PopupUserRegist() {   
    $.ajax({ 
    type: "GET", 
    url: "../Administrator/Questions.xml", 
    success: parseXmlQuestion 
    }); 

    function parseXmlQuestion(xml) 
    { 
     $(xml).find("Question").each(function() 
     { 
      var value=$(this).find('Text').text();  
      $("#sctQuestion"). 
      append($("<option></option>"). 
      attr("value",value). 
      text(value)); 
    }); 
    } 
$("#div_userregist").dialog("open"); 
} 

    $(function() { 

     $("#dialog:ui-dialog").dialog("destroy"); 
     $("#div_userregist").dialog({ autoOpen: false, 
      buttons: { 
       "ok!": function() {             
       } 
     }); 
    }); 

此代码获取XML的成功。

============================================== ============================

代码2

function PopupUserRegist() {   
    $.ajax({ 
    type: "GET", 
    url: "../Administrator/Questions.xml", 
    success: parseXmlQuestion 
    }); 
    function parseXmlQuestion(xml) 
    { 
     $(xml).find("Question").each(function() 
     { 
      var value=$(this).find('Text').text(); 
      $("#sctQuestion"). 
      append($("<option></option>"). 
      attr("value",value). 
      text(value)); 
     }); 
    }  
    $(function() { 

     $("#dialog:ui-dialog").dialog("destroy"); 
     $("#div_userregist").dialog({ autoOpen: true, 
      buttons: { 
       "ok!": function() {             
       } 
     }); 
    }); 
} 

此代码不能获取XML的成功。

============================================== ===================

=========================== ======================================

in Code1:autoOpen:false and in code2:autoOpen:true

in code1获得xml成功,但在代码2中:没有得到xml。

回答

0

当对话框是autoOpened时,它可能在XML已被完全加载和解析之前。我会在parseXmlQuestion()的末尾调用对话框,以便对脚本的所有部分进行计时。

如果需要装载指示器而用户等待,使用这样的:

$('body').append('<div id="ajaxBusy"><p><img src="images/loading.gif"></p></div>'); 

// Ajax activity indicator bound to ajax start/stop document events 
$(document).ajaxStart(function(){ 
    $('#ajaxBusy').show(); 
}).ajaxStop(function(){ 
    $('#ajaxBusy').hide(); 
});