我创建了一个可点击的div列表,以显示一个弹出窗口,其中包含与所点击的项目相关的信息。
每个列表项都假设点击了'列表最后一项的功能。当我删除最后一项时,他们都会假设点击了'新的最后一项的功能。
(例如:我点击列表中的任何项目,会出现一个弹出窗口,其中包含有关最后一项的信息。我通过点击按钮删除它'批准'。当我现在点击任何项目时将出现一个弹出窗口,其中包含新的最后一项的信息)
我想要的是显示所点击项目的信息。
HTML
<ul id='absencesToApproveList'></ul>
<div class="popupBox approveAbsenceBox">
<h3>Absence to Approve</h3>
<div class="absencePhoto"></div>
<p class="noteAbsence" ></p>
<button id="rejectAbsence" style="width:40%; display:block-inline; float:left;">Reject</button>
<button id="approveAbsence" style="width:40%; display:block-inline; float:right;">Approve</button>
</div>
JS
function getAbsencesToApprove_success(tx, results) {
var len = results.rows.length;
if(internetStatus != 'none')
$('#absencesToApproveList').empty()
for (var i=0; i<len; i++) {
var absence = results.rows.item(i);
var absenceType = absence.type;
var absenceText = absence.note;
var absenceDateFrom = absence.absenceFrom;
var absenceDateTo = absence.absenceTo;
var yyyy = absenceDateFrom.split("-")[0];
var mm = absenceDateFrom.split("-")[1];
var dd = absenceDateFrom.split("-")[2];
var newAbsenceBox = $('<div class="absenceBox" style="border: 2px solid black; position:relative;" ')
.append($('<div class="absenceDateBox" style="border: 2px solid black;" ')
.append('<span style="display: inline-block;vertical-align: middle; line-height: normal;" </span ').text(dd + "/" + mm))
.append($('<p class="absenceText" ').text(absenceText));
var newAbsenceBoxItem = $('<li id="absence' + absence.id + 'Item" style="margin-left:-35px;" ').append(newAbsenceBox)
$('#absencesToApproveList').append(newAbsenceBoxItem)
$('#absence' + absence.id + 'Item').click(function(){
$('.absencePhoto').text('photo' + absence.id)
$('.noteAbsence').text(absenceText)
$('#addAbsenceBoxOuter2').show()
$('.approveAbsenceBox').show()
$('#rejectAbsence').click(function () {
$('#addAbsenceBoxOuter2').hide()
$('.approveAbsenceBox').hide()
$('.absencePhoto').empty()
$('.noteAbsence').empty()
})
$('#approveAbsence').click(function () {
$('#addAbsenceBoxOuter2').hide()
$('.approveAbsenceBox').hide()
$('.absencePhoto').empty()
$('.noteAbsence').empty()
db.transaction(
function(tx){
tx.executeSql("DELETE FROM absences WHERE id=" + absence.id +";");
});
db.transaction(
function(tx) {
tx.executeSql("INSERT INTO absences (id,type,absenceFrom,absenceTo,note,aproved,picture) VALUES ("
+ id++
+ ",'"
+ String(absenceType)
+ "','"
+ String(absenceDateFrom)
+ "','"
+ String(absenceDateTo)
+ "','"
+ absenceText
+ "',"
+ "'true'"
+ ","
+ "'image.jpg'"
+ ")");
}, transaction_error);
db.transaction(getAbsences, transaction_error);
})
})
}
$('#absencesToApproveList').show()
}
演示: http://jsfiddle.net/8LdKh/3/ 提前致谢
答案 0 :(得分:1)
变量absence.id&amp;不能使用absenceText,因为它将包含循环中的最后一个值。相反,您必须以某种方式获取所选ID并使用它。这是一个简单的方法。
var selectedID = this.id.substring(7,8); //Will work only for one digits
//$('.absencePhoto').text('photo' + absence.id)
$('.absencePhoto').text('photo' + selectedID)
同样必须得到absenceText值,
//adding id on creation
.append($('<p id="absenceText' + i + '" class="absenceText">').text(absenceText));
//using id to fetch the value
$('.noteAbsence').text($("#absenceText" + selectedID ).text())