2011-05-25 44 views
0

嗨節點參考我下面的腳本來查找一個XML文件,併產生一個有序列表獲取JQ

$(document).ready(function() { 
    $.ajax({ 
     type: "GET", 
     url: "search_action.php" + string, 
     dataType: "xml", 
     success: disxml 
    }); 
}) 

} // function 

function disxml(data) { 

    $(data).find('results').find('client').each(function(row) { 
     name = $(this).find('name').text(); 
     var add1 = $(this).find('address1').text(); 
     var add2 = $(this).find('address1').text(); 
     var pcode = $(this).find('postcode').text(); 
     var num1 = $(this).find('number1').text(); 
     var num2 = $(this).find('number2').text(); 
     var contact = $(this).find('contact').text(); 
     var email = $(this).find('email').text(); 


     display += "<a onclick='populate();'> <b>" + name + "</b> - " + add1 + "<br></a>"; 


    }) 

    divbox.html(display); // draw contents 
} 

function populate() { 


} 

這是它引用

<results> 
    <client> 
    <name>Ascot Racecourse</name> 
    <address1>Berkshire</address1> 
    <address2/> 
    <postcode>SL5 7JX</postcode> 
    <number1/> 
    <number2/> 
    <contact>Alastair Warwick</contact> 
    <email>As per course</email> 
    </client> 
    <client> 
    <name>Aston Villa Football Club</name> 
    <address1>Villa Park</address1> 
    <address2>Birmingham</address2> 
    <postcode>B6 6HE</postcode> 
    <number1/> 
    <number2/> 
    <contact>Andrew Evans </contact> 
    <email>[email protected]</email> 
    </client> 
    <client> 
    <name>Asda 1 Year Celebration</name> 
    <address1>Park In Ipswich</address1> 
    <address2>Ipswich</address2> 
    <postcode>IP</postcode> 
    <number1/> 
    <number2/> 
    <contact/> 
    <email>Jonathan Stephenson</email> 
    </client> 
</results> 

的XML文件時,它一切工作正常,當我有我的名單我有一個鏈接在每一行,當點擊調用函數'填充'

當腳本到達填充函數時,我真的堅持爲我如何引用的particulul一個叫做函數的結果行,我怎麼能找到這個?

感謝您的任何幫助,我希望它是有道理的!

+1

「的具體結果行調用函數」 ......這是很難理解的。 – Pointy 2011-05-25 14:11:38

+0

@Pointy或許OP意味着''或''元素? – 2011-05-25 14:12:36

回答

1

這應該這樣做:

function disxml(data) { 

    $(data).find('results').find('client').each(function(row) { 

     var clientItem = this; 

     var name = $(clientItem).find('name').text(); 
     var add1 = $(clientItem).find('address1').text(); 
     var add2 = $(clientItem).find('address1').text(); 
     var pcode = $(clientItem).find('postcode').text(); 
     var num1 = $(clientItem).find('number1').text(); 
     var num2 = $(clientItem).find('number2').text(); 
     var contact = $(clientItem).find('contact').text(); 
     var email = $(clientItem).find('email').text(); 

     var link = $("<a href='#'><b>" + name + "</b> - " + add1 + "<br/></a>") 

     link.click(function(evt){ 
      evt.preventDefault(); 
      populate(clientItem); 
     }); 

     divbox.append(link); 
    }); 
} 

function populate(item) { 
    alert("Populating " + $(item).find("name").text()); 
}