jQuery事件没有触发。

时间:2014-02-04 11:36:49

标签: javascript jquery jquery-mobile

我上下搜索过该网站,并针对其他未发生事件的人解决了问题。

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

  1. 重复ID - 使用class而不是id来确保
  2. $(准备好)的内部/外部。我试过了两个选项
  3. 它不缺少脚本
  4. 这不是jQuery移动添加类。我认为这是问题,但是told it's not
  5. $(".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);
    }
    

3 个答案:

答案 0 :(得分:1)

您需要使用event delegation,因为元素是动态创建的

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

答案 1 :(得分:1)

尝试:

$(".deleteListItem").on('click',function(){
  <! Whatever you want to do >
});

但是调用getDeviceIDforRemoval()将不起作用,因为它在document.ready

之外

答案 2 :(得分:1)

我认为你必须改变以下功能

function getDeviceIDforRemoval(){
    var $DIDtoRemove = $(this).parent().attr("id");
    $(".device_to_remove").text(devices[$DIDtoRemove].Key); // sends the device ID into confirm submission dialog
}

getDeviceIDforRemoval = function (){
    var $DIDtoRemove = $(this).parent().attr("id");
    $(".device_to_remove").text(devices[$DIDtoRemove].Key); // sends the device ID into confirm submission dialog
}