开发者

reference my XML in a function using 'this'

I have the following script to look up an XML file and produce an ordered list. The result is displayed in a div box and each item in the list can be selected by the user. When an item in the list is clicked, the function 'selected()' will be called.

This all works fine, I can list the data and the user can select. The problem is that I want to populate a form with the XML results and need to reference the selected items , I thought that I could use this but I am getting errors. Can anyone offer some advice please?

I have omitted some of the code that displays the div boxes etc.

function searchaddress() {
    searchstring = $('#nam').val();
    chr = searchstring.length;

    $(document).ready(function() {
        $.ajax({ type: "GET",
                 url: "../241/NEWsearch_action.php?ss=" + searchstring ,
                 dataType: "xml",
                 success: searchxml });
    });
} 

// data returned from AJAX.php 
function searchxml(data) {
    var display = "";
    var msg = "";
    var currentWidth = 25;

    $(data).find('NameSearch').each(function() {
        numres = $(data).find('NameSearch').length;
        coursename = $(this).find('sitelist').text();
        address1 = $(this).find('开发者_如何转开发address1').text();
        address2 = $(this).find('address2').text();
        postcode = $(this).find('postcode').text();
        number1 = $(this).find('number1').text();

        string = "<b> <a onclick='selected(" + **this** + ")' > " + coursename < /br> " ; 
        msg += string;
    }); // find loop
    $('#UniDivBody').html(msg);
}

function selected(e) {
    coursename = $(e).find('sitelist').text();
    // REFERENCE THE SELECTED DATA HERE???
}


Firstly place this line

numres = $(data).find('NameSearch').length ; 

outside the .each call because you are setting numres value everytime the .each function loops through your NameSearch.

Secondly

why not use delegate to delegate your click event.

$('#UniDivBody').delegate('a','click',function(){
    //declare the clicked <a>
    var $element = $(this),
    l_element = this;
    //this is going to pass through the <a> to the selected function
    selected(this);
})

Do you have a live example?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜