jQuery事件未触发。

罗里

我上下搜索了该网站,并针对其他未触发事件的问题进行了故障排除。

我可以确认以下不是问题:

  1. 重复的ID-确保使用类而不是ID
  2. $(就绪)的内部/外部。我已经尝试了两种选择
  3. 它不缺少脚本
  4. 不是jQuery mobile添加类。我以为这是问题,但被告知不是

$(".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);
}
阿伦·P·约翰尼(Arun P Johny)

您需要使用事件委托,因为元素是动态创建的

$("#geraete-list").on('click', ".deleteListItem", getDeviceIDforRemoval);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章