在JQ中获取节点引用

时间:2011-05-25 14:08:37

标签: php jquery ajax

您好我有以下脚本来查找xml文件并生成有序列表

$(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() {


}

这是它引用的xml文件

<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>Info@avfc.co.uk</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>

一切正常,当我有我的列表时,我在每一行都有一个链接,当点击时调用函数'populate'

当脚本进入populate函数时,我真的卡住了我如何引用调用该函数的特定结果行,我该如何找到它?

感谢您的帮助,我希望它有意义!!

1 个答案:

答案 0 :(得分: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());
}