简体   繁体   English

.click()不再起作用

[英].click() not working anymore jquery

I have been using this jQuery before I use $.ajax(); 在使用$.ajax();之前,我一直在使用此jQuery $.ajax(); and it was working good: 运行良好:

    $(document).ready(function(){
    var urlSerilize = 'some link';
    var appList = $("#applications > li > a");
    var appCheck = $('input[type=checkbox][data-level="subchild"]');
    var installbtn = $('#submitbtn');
    var form = [];
    var checked = [];

    //var appList = $(".body-list > ul > li");
    //var appCheck = $('input[type=checkbox][data-level="subchild"]');
    appList.click(function(){
        console.log('here!');
        if($(this).children().find("input").is(":checked")){
            $(this).children().find("input").prop('checked', false);
            $(this).children('form').removeClass('checked');
            $(this).removeClass("li-checked");
            var rmValue = $(this).children('form').attr('id');
            form = jQuery.grep(form, function(value) {
              return value != rmValue;
            });
        }else{
            $(this).children().find("input").prop('checked',true);
            $(this).addClass("li-checked");
            $(this).children('form').addClass('checked');
            form.push($(this).children('form').attr('id'));
        }
        console.log(form);
    });

    installbtn.on('click', function () {
        event.preventDefault();
        jQuery.each( form, function( i, val ) {
            console.log(val);
            var request = $.ajax({
                    url: urlSerilize,
                    type: 'GET',
                    data: $('#'+val).serialize(),
                    success: function( response ) {
                        console.log( response );
                        $('#applications').html();
                        $('#apps_box').html();
                    }
                });

            request.done(function(msg){
                console.log('Ajax done: ' + 'Yeah it works!!!');
            });

            request.fail(function(jqXHR, textStatus){
                console.log('failed to install this application: ' + textStatus);
            });
        });
    });
});

but after I used this ajax code the .click() jQuery event don't work anymore: 但是在我使用此ajax代码后, .click() jQuery事件不再起作用:

$(document).ready(function() {
    /* loading apps */
    //console.log('active');
    var request = $.ajax({
        url: 'some link',
        type: 'GET',
        dataType: 'html',
        data: {id: 0},
    })
    request.done(function(data) {
        console.log("success");
        $('#applications').empty().append(data);
    })
    request.fail(function() {
        console.log("error");
    })
    request.always(function() {
        console.log("complete");
    });
    //end loading apps

    var showmore = $('.showapps');
    showmore.click(function(){
        var parent = $(this).parent('.tv_apps');
        var displayC = parent.children('.body-list').css('display');
        console.log(displayC);
        if (displayC=='none') {
            parent.children('.body-list').show('400');
            $(this).children().find('img').rotate({animateTo: 180});
        }else{
            parent.children('.body-list').hide('400');
            $(this).children().find('img').rotate({animateTo: 0});
        };
    });
});

at first place I though it was because of the ajax loads and don't stop, then i was wrong. 起初我虽然是因为ajax加载而没有停止,但是我错了。 I have tried the window.load=function(); 我已经尝试过window.load=function(); DOM function to load the script after Ajax finish loading and also was wrong. DOM函数在Ajax完成加载后加载脚本,这也是错误的。 So please if there any idea to fix this problem, Thanks. 因此,如果有任何解决此问题的想法,请谢谢。

This is the event I want it to be fixed: 这是我要修复的事件:

appList.click(function(){
    console.log('here!');
    if($(this).children().find("input").is(":checked")){
        $(this).children().find("input").prop('checked', false);
        $(this).children('form').removeClass('checked');
        $(this).removeClass("li-checked");
        var rmValue = $(this).children('form').attr('id');
        form = jQuery.grep(form, function(value) {
          return value != rmValue;
        });
    }else{
        $(this).children().find("input").prop('checked',true);
        $(this).addClass("li-checked");
        $(this).children('form').addClass('checked');
        form.push($(this).children('form').attr('id'));
    }
    console.log(form);
});
showmore.click(function(){

should be 应该

$('.showapps').on('click', function(){    

OR 要么

$(document).on('click','.showapps', function(){

For dynamically added contents, you need to bind events to it. 对于动态添加的内容,您需要将事件绑定到它。

For more info: http://learn.jquery.com/events/event-delegation/ 有关更多信息: http : //learn.jquery.com/events/event-delegation/

Thanks everyone, at last I have found the solution. 谢谢大家,终于找到了解决方案。 It was a question of the DOM , when I use the ready method of jquery it loads an empty ul (without content), so then what I figured out in the first time was correct, all I did is to remove the ready and use a simple function that includes all the .click() events, then call it in request.done(); 这是DOM的问题,当我使用jquery的ready方法时,它会加载一个空的ul (无内容),因此我第一次发现的是正确的,我所做的就是删除ready并使用包含所有.click()事件的简单function ,然后在request.done();调用它request.done(); . This is the solution: 这是解决方案:

function loadInstaller(){
    var urlSerilize = 'some link';
    var appList = $("#applications > li");
    var appCheck = $('input[type=checkbox][data-level="subchild"]');
    var installbtn = $('#submitbtn');
    var form = [];
    var checked = [];

    //...etc

};

$(document).ready(function() {
    /* loading apps */
    //console.log('active');
    var request = $.ajax({
        url: 'some link',
        type: 'GET',
        dataType: 'html',
        data: {id: 0},
    })
    request.done(function(data) {
        console.log("success");
        $('#applications').empty().append(data);
        loadInstaller();
    })

    //...etc
});

I hope this answer will help someone else :) 我希望这个答案对其他人有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM