2016-09-07 60 views
0

我使用一个简单的javascript函数来调用一个php脚本,当我点击一个按钮或链接并返回一些数据。它工作正常,直到我尝试通过该函数调用来自传统数据的数据。让你看我的脚本:Php从ajax调用不能用于多个调用

$(function() { 
 
$(".article_edit").click(function() { 
 
var article_id = $(this).attr("id"); 
 
var dataString = 'article_id='+article_id; 
 
//$('a#'+article_id).removeClass('liked'); 
 
$('#post-body-'+article_id).fadeOut("slow").html('<img src="images/loader.gif" class="loading" />'); 
 
$('a#'+article_id).html('<img src="images/loader.gif" class="loading" />'); 
 
$.ajax({ 
 
\t type: "POST", 
 
\t url: "action/article_enable_edit.php", 
 
\t data: dataString, 
 
\t cache: false, 
 
\t success: function(data){ 
 
    if (data == 0) { 
 
\t alert('Actiunea nu a putut fi realizata!'); 
 
\t } else { 
 
\t $('#post-body-'+article_id).fadeIn("slow").html(data); 
 
\t } 
 
\t } 
 
}); 
 
return false; 
 
}); 
 
}); 
 

 

 
$(function() { 
 
$(".article_edit_close").click(function() { 
 
var article_id = $(this).attr("id"); 
 
var dataString = 'article_id='+article_id; 
 
//$('a#'+article_id).removeClass('liked'); 
 
$('#post-body-'+article_id).fadeOut("slow").html('<img src="images/loader.gif" class="loading" />'); 
 
$('a#'+article_id).html('<img src="images/loader.gif" class="loading" />'); 
 
$.ajax({ 
 
\t type: "POST", 
 
\t url: "action/article_show.php", 
 
\t data: dataString, 
 
\t cache: false, 
 
\t success: function(data){ 
 
    if (data == 0) { 
 
\t alert('Actiunea nu a putut fi realizata!'); 
 
\t } else { 
 
\t $('#post-body-'+article_id).fadeIn("slow").html(data); 
 
\t } 
 
\t } 
 
}); 
 
return false; 
 
}); 
 
});
<!-- First button--> 
 
<a class="color-transition article_edit" href="#" id="'.$id.'"><i class="fa fa-pencil-square-o"></i></a> 
 

 
<!-- When i click it the content will be replaced with another one containing this--> 
 
<!-- output from action/article_enable_edit.php --> 
 
\t \t \t \t <a class="color-transition article_edit_save" href="#" id="'.$id.'" ><i class="fa fa-save"></i></a> 
 
\t \t \t \t <a class="color-transition article_edit_close" href="#" id="'.$id.'" ><i class="fa fa-ban"></i></a> 
 

 
<!-- Now, if i click on any link, for example: .article_edit_close, the function will not work. --> 
 
<!-- i should have an output from action/article_show.php --> 
 
<!-- If i put these oow links on the index.php page, they worked, but that is not what i need.-->

为什么我不能把从内容至极的功能被另一个函数叫什么名字?它们不会同时发生......

回答

1

您需要为动态元素使用.on绑定链接/按钮的click事件。

所以你的点击事件代码会是这个样子,

$('body').on('click','.article_edit_close',function(e){ 
    // Your code goes here... 
}); 

。对()附加一个事件处理函数的一个或多个事件来选择的元素。

0

使用

$(document).on("click",".article_edit",function(){ /* ... */ }); 

,而不是

$(".article_edit").click(); 
尝试