重新加载$(文档).ready(function()重新加载ajax页面

我在ajax请求中重新加载html页面后有一个常见的js文件,我无法访问此文件中的函数, $(document).ready(function()之间常见的JS函数如何访问它们并触发常用文件中的函数示例

COMMON JS:

$(document).ready(function() { $(".agree_btn").click(function(){ alert(123); }); }); 

phtml页面中的function

 $('.loadMoreAnswers').live('click', function(event) { var location_id = $(this).attr('location_id'); var counter= $(this).attr('counter'); $('#loadingAnswer').show(); $.ajax({ type: 'POST', url: '/daleel/loadmore', data: 'location_id='+location_id+'&part='+'answers'+'&answerCounter='+counter, //with the page number as a parameter success: function(msg){ if(msg.length!=0) //if no errors { $(this).parent().load("view") $('#loadingAnswer').remove(); counter+=5; $('#profile-page-answer').append(msg); } else $("#loadingAnswer").remove(); }, dataType: 'html' }); }); 

它渲染HTML像这样:

  Agree  

但是当我点击这个链接时,它不会运行Common JS文件中的函数

在ajax成功中重新绑定click事件处理程序

 success: function(msg){ //your code $(".agree_btn").bind('click'); } 

或者你可以使用delegate jQuery版本低于1.7喜欢

 $(document).delegate(".agree_btn",'click',function(e){ //your code }); 

或者你正在使用jQuery版本1.7+使用方法

 $(document).on("click",".agree_btn",function(e){ //your code }); 

不要使用.live其已弃用的文档

从jQuery 1.7开始,不推荐使用.live()方法。 使用.on()附加事件处理程序。 旧版jQuery的用户应该使用.delegate()而不是.live()。

在常见的js文件中使用live() 。 例如

 $('selector').click(function(){ //function body }) 

而不是这个用途

 $('selector').live('click', function(){ //function body }) 

在ajax完成后嵌入document.ready函数。 它将在ajax调用后在页面上重新注册脚本。

例如:

 $.ajax({ url: 'deleteEntry', data: {'ids': JSON.stringify(getList)}, async: true, success: function (data) { location.reload(); }, error: function (data) { alert('error'); }, complete: function (data) { //add your script in body here } });