2017-10-18 36 views
1

我在codeigniter项目中触发事件时遇到问题。我想在我的页面中动态加载表格。该表通过ajax调用从我的控制器返回。我想在该表中的一个范围内触发点击事件。我在很多方面都尝试过这种方式,但它不起作用。我在这里附上我的代码。请帮助我找到解决方案。在Ajax响应元素上触发JQuery事件

控制器:鉴于

$str = '<table> 
     <tr> 
      <td class="cost date" id="date_'.$request['SupplyID'].'"> 
       <span class="editable" style="display: none;"> 
        <input type="text" class="expiry" autocomplete="off" data-date-id="date_'.$request['SMSSupplyID'].'">          
       </span> 
       <span class="cnt" style="display: inline;">Choose</span> 
      </td> 
      </tr> 
     </table>'; 
echo $str; 

脚本:

function loadmore() 
    { 
     var url; 

     url = '/SMS/SMS/loadMoreRequests'; 

     $.ajax({ 
      type: 'post', 
      url: url, 
      data: { 
       limit:val 
      }, 
      success: function (response) { console.log(response); 

       $("#requestListTable").html(response); 
       val +=10; 
      } 
     }); 
    } 

$('.date').find('.cnt').on('click', function(){ 
    console.log(1);  // for testing 
}); 

我尝试了以下的变化,但没有用

1)

$('.date').find('.cnt').live('click', function(){ 
    console.log(1);  // for testing 
}); 

2)

$('.date').find('.cnt').click(function(){ 
    console.log(1);  // for testing 
}); 
+0

试试我的回答我希望它能解决你的问题。 –

回答

1

您需要将事件委托给动态创建的元素。

$(document).on('click','.cnt',function(){ 
    alert("Processing comes here"); 
}); 
+0

工作正常。谢谢ketan。 – geeth

+0

@geeth谢谢你接受我的回答。很高兴帮助你。 –

2

你可以试试这个

$(document).find(".cnt").on("click",function(){ 
    console.log(1);  // for testing 
}); 
+0

仍然不起作用 – geeth

+0

检查我编辑的答案@geeth –

+0

这不起作用 –

2
$("container for the table").on("click", ".cnt", function() { 


}); 

您可以使用事件代表团此。将事件绑定到文档上准备好的DOM上的父元素。或者你可以简单地使用,

$("body").on("click", ".cnt", function() { 


}); 
相关问题