2017-10-18 39 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() { 


}); 
相關問題