我上下搜索了该网站,并针对其他未触发事件的问题进行了故障排除。
我可以确认以下不是问题:
该$(".deleteListItem").click(getDeviceIDforRemoval);
是不触发事件
我强烈怀疑某个东西在准备就绪之前会被调用,反之亦然,但无法弄清楚在哪里。奇怪的是。真正困扰我的是跟进和类似事件$("#remove_device").click(removeDevices);
正在触发。我已经比较了两者,看是否有差异,但看不到它们。
任何帮助,甚至提示将不胜感激。相关代码如下:
$(document).ready(function() {
start();
loadConfig();
loadDevices();
$(".deleteListItem").click(getDeviceIDforRemoval); //why isn't this firing??!?!?
$("#remove_device").click(removeDevices); //fires
});
...
...
var devices = [];
function loadDevices(onSuccess,onError,onWrongPassword) {
Server.queryDevices(function (data) {
devices = data;
showDevices();
if(onSuccess!==undefined)onSuccess();
},onError,onWrongPassword);
}
function showDevices() { //works fine
for (var i = 0; i < devices.length; i++) {
var value = devices[i].Value;
var geraete_id = devices[i].Key;
if (value.Name != "") { // if name exists, use that as heading, else, use UDID
$("#geraete-list").append("<li id='geraete-"+i+"'><a href='#'><h5>" + value.Name + "</h5>"
+ "<p>" + value.LastUsedBy + "</p>"
+ "<p>" + geraete_id + "</p>"
+ "<p>Zuletzt Verwendet: " + formatDate(jsonToDate(value.LastUsed)) + "</p></a>"
+ "<a href='#confirm_device_removal' **class='deleteListItem'** data-rel='dialog'></a></li>");
}
else {
$("#geraete-list").append("<li id='geraete-"+i+"'><a href='#'><h5>" + geraete_id + "</h5>"
+ "<p>" + value.LastUsedBy + "</p>"
+ "<p>Anonymous User</p>"
+ "<p>Zuletzt Verwendet: " + formatDate(jsonToDate(value.LastUsed)) + "</p></a>"
+ "<a href='#confirm_device_removal' **class='deleteListItem'** data-rel='dialog'></a></li>");
}
}
$("#geraete-list").listview("refresh");
}
function getDeviceIDforRemoval(){
var $DIDtoRemove = $(this).parent().attr("id");
$(".device_to_remove").text(devices[$DIDtoRemove].Key); // sends the device ID into confirm submission dialog
}
function removeDevices(){
var geraetID = $(".device_to_remove").text(); //retrieves the DID inserted by above function
removeDevice(geraetID,loadDevices);
}
function removeDevice(udid,onSuccess){
Server.removeDevice(udid,onSuccess);
}
您需要使用事件委托,因为元素是动态创建的
$("#geraete-list").on('click', ".deleteListItem", getDeviceIDforRemoval);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句