繁体   English   中英

从ajax呼叫的php无法在多个通话中使用

[英]Php called from ajax not working on multiple calls

当我单击按钮或链接并返回一些数据时,我正在使用一个简单的javascript函数来调用php脚本。 直到我尝试通过该函数从有铅输出数据中调用数据之前,它都可以正常工作。 让我们看看我的脚本:

 $(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({ type: "POST", url: "action/article_enable_edit.php", data: dataString, cache: false, success: function(data){ if (data == 0) { alert('Actiunea nu a putut fi realizata!'); } else { $('#post-body-'+article_id).fadeIn("slow").html(data); } } }); 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({ type: "POST", url: "action/article_show.php", data: dataString, cache: false, success: function(data){ if (data == 0) { alert('Actiunea nu a putut fi realizata!'); } else { $('#post-body-'+article_id).fadeIn("slow").html(data); } } }); 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 --> <a class="color-transition article_edit_save" href="#" id="'.$id.'" ><i class="fa fa-save"></i></a> <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.--> 

为什么我不能从内容调用另一个函数调用的函数? 它们不会同时发生。

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

因此,您的点击事件代码将如下所示,

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

.on()将一个或多个事件的事件处理函数附加到所选元素。

尝试使用

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

代替

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM