我有以下代码:

console.log($(".resultLike"));
$(".resultLike").on("click", function (event) {
    alert('test');
    event.stopPropagation();
    alert('test1');
    value['clicked'] = 1;
    $.ajax({
        url: 'scripts/php/userProfile.php',
        data: {
            action: value
        },
        type: 'post',
        dataType: 'json',
        success: function (profileData) {
            if (profileData == 'removed') {
                $(me).children('.resultInside')
                .children('.resultData')
                .children('.resultLike').html('Favourite?');
            } else if (profileData == 'notLoggedIn') {
                $(me).children('.resultInside')
                .children('.resultData')
                .children('.resultLike').html('Login to add');
            } else {
                $(me).children('.resultInside')
                .children('.resultData')
                .children('.resultLike').html('un-Favourite');
            }
        }
    });
});


我的期望是,当您单击div resultLike时,它将执行function()。但是,它什么也不做。我在那里有两个alert()调用,但都没有被调用。 console.log()的输出如下:

[
<div class=​"resultLike searchBarGameLike">​</div>​
]


证明它已被放到页面上。任何帮助,将不胜感激,谢谢。

编辑:

我认为应该提到的是,我实际上正在使用两个.on()事件。
这实际上是我的全部代码。问题出在
$("body").on("click", ".resultLike", function(){
行,它不起作用。

$searchedGamesContainer.on(
    "click",
    ".box",
    function(event){
        if(!$displayLock){
            $displayLock = true;
            var description;
            var user_id;
            if($(this)[0].style.width == '75%'){
                var org_img = $(this).children(".resultInside").find("img").attr("src");
                $(this).children(".resultInside").append("<img src='"+org_img+"' id='imgId'/>");
                $(this).children(".resultInside").css('height','auto');
                $(this).css('width', '18%');
                $(this).css('height', 'auto');
                $(this).find(".resultData").fadeOut('fast');
                setTimeout(function(){$searchedGamesContainer.masonry('reload');},300);
                setTimeout(function(){$displayLock = false;},1000);
            }else{
                var me = this;
                var pos;
                largeImage=  new Image();
                value['name']=$(me).find("p").html();
                oldImage = $(this).find("img").attr("src");
                for(var i = 0; i<$searchBarGames.length; i++){
                    if($searchBarGames[i][5] == value['name']){
                        pos = i;
                        break
                    }
                }

                description = $searchBarGames[pos][2];
                $(me).find("img").hide();
                largeImage.src = $searchBarGames[pos][4];
                $(me).find("img").attr("src",largeImage.src);
                $(me).children(".resultInside").css('height','400px');
                $(me).css('width', '75%');

                $(me).children(".resultInside").html("\
                    <div class='resultData'>\
                        <div class='resultImg'><img src='"+ largeImage.src +"'></div>\
                        <div class='resultName'>" + value['name'] + "</div>\
                        <div class='resultDesc'>" + description +"</div>\
                        <div class='wikiLink searchBarGameWiki'><a href='http://wikipedia.org/wiki/" + value['name'] + "'>Wikipedia</a></div>\
                        <div class='resultLike searchBarGameLike'></div>\
                    </div>");
                value['clicked']=0;

                $.ajax({
                    url:'scripts/php/userProfile.php',
                    data:{action:value},
                    type: 'post',
                    dataType: 'json',
                    success:function(profileData){
                        //logic
                    }
                });

                console.log($(".resultLike"));

                $("body").on("click", ".resultLike", function(){
                        alert('test');
                        event.stopPropagation();
                        alert('test1');
                        value['clicked']=1;
                        $.ajax({
                            url:'scripts/php/userProfile.php',
                            data:{action:value},
                            type: 'post',
                            dataType: 'json',
                            success:function(profileData){
                                //logic
                            }
                        });
                    }
                );
            }
        }
    }
);

最佳答案

尝试

$("body").on("click", ".resultLike", function(){
      // your code goes here.
});


如果动态添加div.resultLike,则您的实现可能不受约束

09-25 20:11