2011-10-01 6 views
0

로드 및 조각을 사용하여 몇 가지 선택 목록에 대한 옵션을로드하고 있습니다. 몇 가지 목적으로 jQuery로드 단편

내가 무엇을 가지고 :

$(document).ready(function(){ 
     $("#select1").load("ts.xml #select1", 
       function (responseText, textStatus, XMLHttpRequest) { 
        if (textStatus == "success") { 
         alert("Loaded select 1"); 

        } 

     $("#select2").load("ts.xml #select2", 
       function (responseText, textStatus, XMLHttpRequest) { 
        if (textStatus == "success") { 
         alert("Loaded select 2"); 

        }  
    }); 

을 그리고 내 HTML은 다음과 같습니다

ts.xml은 다음과 같습니다
<li id="select1" class="left"></li> 
<li id="select2" class="left"></li> 

:

<select id="select1"> 
<option>Lorem</option> 
<option>Ipsum</option> 
<option>Lorem Ipsum</option> 
</select> 
<select id="select2"> 
<option>Lorem</option> 
<option>Ipsum</option> 
<option>Lorem Ipsum</option> 
</select> 

한 번 ts.xml을로드하고 fragm을 계속 검색 할 수 있습니까? 그것 밖으로 ent? 조각을 검색 할 때마다 ts.xml로드가 아닌 성공을 경고하고 싶습니다.

덕분에 많이

답변

1

로드 번, 캐시, 다음 캐시 된 버전 사용

$(document).ready(function(){ 
    var response; 
    $("#select1").load("ts.xml #select1", 
     function (responseText, textStatus, XMLHttpRequest) { 
      if (textStatus == "success") { 
       response = responseText; // this will be your xml response 
       // now you can use 'response' anywhere inside the doc.ready function 
      } 
     });  
}); 
+0

xml 캐시로 '응답'을 사용하여 select2를 불러올 수 있습니까? 나는 그것의 개념을 이해했다. 그러나 나는 그것을 만드는 법을 모른다. 그러므로 나는 질문했다. 고마워요. – jQuerybeast

+0

@ adeneo의 대답은 그것을하는 방법 일 것입니다. 'response'를 jQuery 객체 (response = $ (responseText))로 변환하면 jQuery 메서드를 사용하여이를 파싱 할 수 있습니다. 그래서 # select2를 채울 필요가있을 때, 당신은 단지'response' 변수를 잡고 그것을 조작합니다. – swatkins

0

을 나는 아마 다시 기본 $ 아약스 기능에 가서 이런 일을 할 것이라고 생각합니다.

var xmlData; 

function getXML() { 
    $.ajax({ 
    type: "GET", 
    url: "ts.xml", 
    dataType: "xml", 
    error: function() { 
    $("#mydiv").append("<p>File Not Found!</p><br />"); 
    }, 
    success: function(xml){ 
     xmlData = $(xml); 
    } 
}); 

이제 어디서나 데이터를 검색 할 수 있습니다. 적어도 그렇게 생각하지는 않지만 테스트하지는 못하는 이유는 무엇입니까?

var Searchvalue = "Lorem"; 

xmlData.find("select1").find("option:contains('" + Searchvalue + "')").each(function(){ 
     // do something 
     });