ajax调用捕获事件不工作的Bootstrap开关内的数据表插件在响应视图


ajax call catch event not working on Bootstrap switch inside datatables plugin on responsive view

Ajax调用catch事件不工作的Bootstrap切换内部的数据表插件在响应视图

$('.switch').bootstrapSwitch();
$('.switch').on('switchChange.bootstrapSwitch', function () 
{
    var id =$(this).attr("attrid");  
    $.ajax({
        type:'POST',
        url:'<?php echo base_url();?>parties/party_status_ajax',
        data:{'id':id,'checked':$(this).bootstrapSwitch('state')},
        success: function (data)
        {
            $('.close').click();
            $('#successmessage').html(data); 
        }
    });
 });

问题可能是您将事件绑定到html中不存在的元素,当该代码运行时,您需要使用委托事件来将其绑定到数据表的后续元素

就像

下面
$( "#dataTable tbody" ).on( "switchChange.bootstrapSwitch", ".switch", function() {
   var id =$(this).attr("attrid");  
    $.ajax({
        type:'POST',
        url:'<?php echo base_url();?>parties/party_status_ajax',
        data:{'id':id,'checked':$(this).bootstrapSwitch('state')},
        success: function (data)
        {
            $('.close').click();
            $('#successmessage').html(data); 
        }
    });
});