2011-04-13 178 views
0

我有两个php文件,其中一个是通过url执行的,该文件使得ajax调用第二个php文件。一些AJAX结果返回HTML内容作为结果,其中有一些JavaScript事件。这些事件并不奏效,所以我每次调用Ajax调用JS事件返回HTML内容时都会包含js文件。我不认为这是正确的方法。 eventhough 我使用Ajax的 '活' 事件返回的HTML内容怀疑在PHP文件中包含js文件

但什么是正确的方式

function show_regfunc_to_box(this_id){ 
var enc_item_id = this_id; 
enc_item_id = enc_item_id.split("--")[1]; 
regfunc_to_item_id_hidden = '#regfunc_to_item_id_hidden--'+enc_item_id; 
item_id = $(regfunc_to_item_id_hidden).val(); 
var current_action_regfunc_to_id = '#current_action_regfunc_to_id--'+enc_item_id; 
if(($(current_action_regfunc_to_id).css('display'))=='none'){ 
    $.getJSON('./ajax_funcs.php?func=regfunc_to_box',{item_id_regfunc_to:item_id,enc_item_id:enc_item_id},function(data){ 
     $('.current_action_regfunc_to_and_chfunc').hide(); 
     $(current_action_regfunc_to_id).center_box(); 
     $.post('./ajax_funcs.php?func=regfunc_to_box',{enble_js_file:true}); 
     $(current_action_regfunc_to_id).html(data.regfunc_box_html); 

     //$(current_action_regfunc_to_id).center_box(); 
     $(current_action_regfunc_to_id).show(); 
    }); 

} 
//cls_i_item_options(); 

}

elseif($_REQUEST['func']=='regfunc_to_box'){ 
    require_once('./js/jq_funcs.js'); 
    ajax_fcs::regfunc_to_box($_REQUEST['item_id_regfunc_to'],$_REQUEST['enc_item_id']); 
} 
+0

向我们展示一些代码,BC一些你写了,可能没有赚那么多道理,直到我们可以撒尿你的意思 – Neal 2011-04-13 16:16:44

+0

现场甚至应该工作为jj加载内容。如果你粘贴我们的代码,我们可能会帮助 – 2011-04-13 16:18:02

回答

0

包括JS使用Ajax返回的HTML真的不是最好的做法。相反,在您的根文件(您的第一个文件)中包含javascript文件,并确保第二个文件只返回HTML。

从这里开始,这只是Javascript的问题。从Ajax请求获取的HTML所涉及的事件将不起作用,因为浏览器不知道有任何事件附加到该DOM部分。因此,要完成这项工作,请重新绑定Javascript回调函数中的所有事件(即在Ajax请求完成时执行的函数)。

一个例子,如果你正在使用jQuery:

$.get('second_page.php', function(data){ 
// that is callback function, let's say 
// you are getting a div with id="test" 
// as returned HTML 
$('body').append($(data)); 

$('#test').bind('some_event', function(e){ 
    // and that is event callback 
}); 
}); 
+0

感谢很多人。有效。谢谢 – 2011-04-13 17:09:37