duanqie5741 2014-03-30 08:33
浏览 44
已采纳

使用jquery和动态内容处理事件

I have a table with a check box which allows to edit the columns in the chosen row. Whenever I click the check box, a dialog pop's out, as it should be. The problem begins when I dynamically change the page content with Ajax (When I change the page of the table). Whenever I click on the 'next' button (to get the next rows of the table), the content changes dynamically and the dialog doesn't pop out when I click on the edit check box. I tried various ways to handle the change but it seems to me that I can't get it right.

Here is the Next button code:

    $('#Next').click(function(){
        if(Number($('#Next').attr('n'))+1 >= Number($('#Next').attr('tn'))){
            $('#Next').removeAttr('class');
            $('#Next').attr('class','next disabled');
        }
        var tmp = Number($('#Next').attr('n'));
        $('#Next').removeAttr('n');
        $('#Next').attr('n',(tmp+1).toString());
        $.get("receive.php",{Page: $('#Next').attr('n')}).done(function(data){
            $('tbody').html(data);
            $('#Previous').removeAttr('class');
            $('#Previous').attr('class','previous');
            $.PutRating();
        });

    });

The data I receive from the php function is the new table content, but then, I can't use the edit check box. Here is the code for it:

    $('input.cb').on('change',function(){
        $('input.cb').not(this).prop('checked',false);
        BootstrapDialog.show({
            title: 'Choose Option',
            message: 'Select an option for: <strong>'+$(this).attr('n')+'</strong>. Be careful!',
            draggable: true,
            type: 'type-warning',
            data:{
                'name': $(this).attr('n')
            },
            buttons: [{
                label: 'Edit',
                icon: 'glyphicon glyphicon-pencil',
                cssClass: 'btn-warning',
                action: function(dialogItself){
                    dialogItself.close();
                    $('#ShowForm').trigger('click');
                    $('#ShowForm').hide();
                    $('#Add').text('Save');
                    $('#Add').attr('class','btn btn-success pull-right');
                    $('#Close').show();
                    $.post('receive.php',{GetRow: 'true',Name: dialogItself.getData('name')}).done(function(data){
                        var values = data.split(':');
                        $('#Type').val(values[0]);
                        $('#Name').val(values[1]);
                        $('#Hobby').val(values[2]);
                        $('#Favorite_color').val(values[3]);
                        $('#Favorite_food').val(values[4]);
                        $('#Notes').val(values[5]);
                    });
                }
            }, {
                icon: 'glyphicon glyphicon-trash',
                label: 'Delete',
                cssClass: 'btn-danger',
                action: function(){

                }
            }]
        });
    });

input.cb is the check box input. I tried the .on function, but It didn't work, I guess I didn't understand it.

  • 写回答

1条回答 默认 最新

  • doufuhao8085 2014-03-30 08:56
    关注

    Yes that's happening because the elements get added dynamically . You have to do Event delegation
    If all your dynamic content is added inside a static container div say it has id #container maybe like this

     <div id="container">  
        // all your dynamic content gets added inside here 
     </div>
    

    then you must use this syntax -- >

      $('#container').on('change','input.cb',function(){ 
       // your code 
      });
    

    The parent div comes first and then the event and then the element you want to listen for . Normal language translation would be "Listen for a change event for input.cb inside #container "
    By using this it will get triggered even if that element you are listening for is added dynamically because we did event delegation here and referred to a static parent div in our case its #container(Always try to refer the the closest static parent which would wrap the whole dynamic portion just for performance sake)

    You can replace the trigger event like click , change accordingly ..

    Note : Make sure that the container div is not dynamically coming from ajax it must be static .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探