使用“this”在函数中引用我的 XML

发布于 2024-12-09 11:20:14 字数 1421 浏览 1 评论 0原文

我有以下脚本来查找 XML 文件并生成有序列表。结果显示在 div 框中,用户可以选择列表中的每个项目。当单击列表中的项目时,将调用函数“selected()”。

这一切都工作正常,我可以列出数据并且用户可以选择。问题是我想用 XML 结果填充表单并需要引用所选项目,我认为我可以使用 this 但出现错误。有人可以提供一些建议吗?

我省略了一些显示 div 框等的代码。

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???
}

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???
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥︴琐丶欲为 2024-12-16 11:20:14

首先将此行放在

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

.each 调用之外,因为每次 .each 函数循环遍历 NameSearch 时都会设置 numres 值。

其次

为什么不使用委托来委托您的点击事件。

$('#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);
})

你有活生生的例子吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文