使用“this”在函数中引用我的 XML
我有以下脚本来查找 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先将此行放在
.each
调用之外,因为每次.each
函数循环遍历 NameSearch 时都会设置numres
值。其次
为什么不使用委托来委托您的点击事件。
你有活生生的例子吗?
Firstly place this line
outside the
.each
call because you are settingnumres
value everytime the.each
function loops through your NameSearch.Secondly
why not use delegate to delegate your click event.
Do you have a live example?