jQuery不止一次调用函数 [英] jQuery more than once function called
问题描述
我正在创建一个移动应用,并且正在调用函数getItem传递data-trnote val.
I am creating an mobile app and I am calling the function getItem passing data-trnote val.
function getTitles() {
$(document).ready(function(e){
var list = $('#recent'),
items = [];
$.mobile.notesdb.transaction(function(t) {
t.executeSql('SELECT buildingcode, buildingaddress FROM buildings ORDER BY buildingaddress ASC', [], function(t, result) {
var i,
len = result.rows.length,
row;
if (len > 0 ) {
for (i = 0; i < len; i += 1) {
row = result.rows.item(i);
items.push('<li><a href="#display" data-trnote="' + row.buildingcode + '">' + row.buildingaddress + '........' + row.buildingcode + '</a></li>');
}
list.html(items.join('\n'));
list.listview('refresh');
$('a', list).live('click', function(e) {
getItem($(this).attr('data-trnote'));
});
$('#entries').show();
} else {
$('#entries').hide();
}
})
});
});
}
getItem代码如下:
The getItem code is as follows:
function getItem(buildingcode) {
alert(buildingcode);
$(document).ready(function(){
var list = $('#recentflats'),
items = [];
$.mobile.notesdb.transaction(function(t) {
t.executeSql('SELECT buildingaddress, buildingcode FROM buildings WHERE buildingcode = ?',[buildingcode], function(t, resultbuilding) {
var myrow;
myrow = resultbuilding.rows.item(0);
$('#display h1').text(myrow.buildingaddress);
})
});
$.mobile.notesdb.transaction(function(t) {
t.executeSql('SELECT DISTINCT flatdescription, flatname, buildingcode FROM bill WHERE buildingcode = ?',[buildingcode], function(t, resultflat) {
var i,
len = resultflat.rows.length,
row;
if (len > 0 ) {
for (i = 0; i < len; i += 1) {
row = resultflat.rows.item(i);
items.push('<li><a href="#displayflat" data-flat="' + row.flatname + '" data-description="' + row.flatdescription + '">' + row.flatdescription + '...' + row.flatname + '</a></li>');
}
list.html(items.join('\n'));
list.listview('refresh');
$('a', list).live('click', function(e) {
getItem1($(this).attr('data-flat'), $(this).attr('data-description'));
});
$('#entriesflat').show();
} else {
$('#entriesflat').hide();
}
})
});
});
}
两个函数都创建动态列表视图.
both functions create dynamic listviews.
getTitles函数显示公司的建筑物,而getItem显示所选建筑物的单位.
The getTitles function displays the buildings of a company while getItem displays the flats of the selected building.
我包括了alert(buildingcode);
来找出问题所在,但我不明白哪里出了问题.
I include alert(buildingcode);
to find out the problem but I cannot understand what is the wrong.
第一次一切都很好.当我返回到getTitles并前进到getItem时,警报显示两次...当我返回并转发警报显示3次,所以继续进行4次... 5次...
The first time everything is ok. When I go back to getTitles and forward to getItem the alert displays twice... when I go back and forward the alert display 3 times and so go on 4 times... 5 times...
并将此后的所有代码重复作为警报...
and all the code from this point repeated as the alert...
怎么了?
推荐答案
我认为它是使用live
的click
处理程序. live
在document
或body
上附加事件处理程序,并侦听我们通过的选择器.每当您调用getTitles
时,它将附加一个新的处理程序.
I think it is the click
handler using live
. live
attachs event handler on the document
or body
and listens to the selector which we pass. Whenever you call getTitles
it will attach a new handler.
看看您的代码,无需使用live
即可,只需使用click
处理程序即可.
Looking at your code there is no need of using live
just use click
handler it will work fine.
在getTitles
$('a', list).click(function(e) {
getItem($(this).attr('data-trnote'));
});
与getItem
方法相同
$('a', list).click(function(e) {
getItem1($(this).attr('data-flat'), $(this).attr('data-description'));
});
这篇关于jQuery不止一次调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!