javascript - 从 ajax 调用的 PHP 无法处理多个调用

标签 javascript php jquery ajax

当我单击按钮或链接并返回一些数据时,我使用一个简单的 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绑定(bind)链接/按钮的click事件。

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

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

.on() attach an event handler function for one or more events to the selected elements.

关于javascript - 从 ajax 调用的 PHP 无法处理多个调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39364741/

相关文章:

javascript - 访问未命名对象的属性(对象内)

javascript - CSS、JavaScript、HTML - 所见即所得编辑器中的空段落和错误代码

javascript - 如何从 app.js 处理 Express 4/nodejs 中的获取/发布,将数据从表单传递到显示结果的新页面

javascript - Rails 3. 如何使用 jQuery 向订单项添加税费?

javascript - 使用 Node.js 进行视频游戏流传输

javascript - 在电子商务网站中添加多个商品到购物车(add to cart)

php - 需要有关 mysql 查询的帮助

jquery - 自动播放属性会导致iframe错误(youtube)

php - 简单的 Ajax 过滤器不返回结果

javascript - jQuery:绑定(bind)到图像 onload 的函数获取旧的 height() 信息