jQuery多值更新问题
我有下面的jQuery代码,它不能像我期望的那样工作。我肯定我做错了什么,但我看不出来。基本上,如果博客没有发布,当我点击图片时,它会访问我的数据库并相应地更新它。所有这些都很好jQuery多值更新问题,jquery,Jquery,我有下面的jQuery代码,它不能像我期望的那样工作。我肯定我做错了什么,但我看不出来。基本上,如果博客没有发布,当我点击图片时,它会访问我的数据库并相应地更新它。所有这些都很好 我点击。未发布 .not_published的内容将使用显示ajax-loader.gif的图像标记进行更改 当服务器返回一条成功消息时,我会用一个新内容更新“not#u published parent”的html,其中包括将.not_published替换为.published 当我点击.not_publishe
警报(“message”);
,它就可以正常工作
列表第3点不起作用。我无法找出原因。请参见下面的代码:
jQuery代码:
$(document).on('click','.not_published', function(){
var ID = $(this).siblings("p").text();
$(this).html("<img style=\"padding-left:15px;\" src=\"/img/admin/ajax-loader.gif\">");
$.ajax({
url: "/posts/publish/"+ID,
type: "post",
data: '',
success: function(responseText, statusText, xhr, $form){
$(this).parent().html("<span class=\"published\"><img style=\"width:20px;\" src=\"/img/admin/checkmark_green.png\"></span><p style=\"display: none\">ID</p>");
},
error: function(responseText){
alert(responseText);
}
});
});
$(文档).on('click','not_published',function(){
var ID=$(this.sibbines(“p”).text();
$(this.html(“”);
$.ajax({
url:“/posts/publish/”+ID,
类型:“post”,
数据:“”,
成功:函数(responseText、statusText、xhr、$form){
$(this.parent().html(“
”;
},
错误:函数(responseText){
警报(responseText);
}
});
});
HTML代码
<div id="publish">
<span id="not_published">
<img style="width:20px" src="/img/admin/checkmark_red.png">
</span>"
<p style="display: none">1</p>
</div>
"
1
您正在使用id“发布”和“未发布”。尝试使用类,而不是像.not#u published那样使用多个#not#u published
并在发送前检查var ID您正在使用ID“发布”和“未发布”。尝试使用类,而不是像.not#u published那样使用多个#not#u published
并在发送之前检查var ID将元素存储在AJAX请求之外的变量中,但在执行之前。。。内部成功处理程序日志
将此
发送到控制台。不是你想的那样
还将ID更改为类,如注释中所述
$(document).on('click','.not_published', function(){
var ID = $(this).siblings("p").text();
var $el=$(this).html("<img style=\"padding-left:15px;\" src=\"/img/admin/ajax-loader.gif\">");
$.ajax({
url: "/posts/publish/"+ID,
type: "post",
data: '',
success: function(responseText, statusText, xhr, $form){
/* change $(this) to $el defined above*/
$el.parent().html("<span class=\"published\"><img style=\"width:20px;\" src=\"/img/admin/checkmark_green.png\"></span><p style=\"display: none\">ID</p>");
},
error: function(responseText){
alert(responseText);
}
});
});
$(文档).on('click','not_published',function(){
var ID=$(this.sibbines(“p”).text();
var$el=$(this.html(“”);
$.ajax({
url:“/posts/publish/”+ID,
类型:“post”,
数据:“”,
成功:函数(responseText、statusText、xhr、$form){
/*将$(this)更改为上面定义的$el*/
$el.parent().html(“
”;
},
错误:函数(responseText){
警报(responseText);
}
});
});
在AJAX请求之外的变量中存储元素,但在执行之前。。。内部成功处理程序日志将此
发送到控制台。不是你想的那样
还将ID更改为类,如注释中所述
$(document).on('click','.not_published', function(){
var ID = $(this).siblings("p").text();
var $el=$(this).html("<img style=\"padding-left:15px;\" src=\"/img/admin/ajax-loader.gif\">");
$.ajax({
url: "/posts/publish/"+ID,
type: "post",
data: '',
success: function(responseText, statusText, xhr, $form){
/* change $(this) to $el defined above*/
$el.parent().html("<span class=\"published\"><img style=\"width:20px;\" src=\"/img/admin/checkmark_green.png\"></span><p style=\"display: none\">ID</p>");
},
error: function(responseText){
alert(responseText);
}
});
});
$(文档).on('click','not_published',function(){
var ID=$(this.sibbines(“p”).text();
var$el=$(this.html(“”);
$.ajax({
url:“/posts/publish/”+ID,
类型:“post”,
数据:“”,
成功:函数(responseText、statusText、xhr、$form){
/*将$(this)更改为上面定义的$el*/
$el.parent().html(“
”;
},
错误:函数(responseText){
警报(responseText);
}
});
});
为什么要通过ajax加载图像,而不直接加载图像,只需更改源代码?还有,既然可以更改具有相同效果的类,为什么还要更改id呢。id“必须”是唯一的,才能正常工作。try HADI的SolutionId必须是唯一的。使用类为什么要通过ajax加载图像,而不是直接加载图像,而只是更改源代码?还有,既然可以更改具有相同效果的类,为什么还要更改id呢。id“必须”是唯一的,才能正常工作。try HADI的SolutionId必须是唯一的。使用类我已经将所有内容更改为类,但仍然存在相同的问题。。。我已经用更改更新了我的问题,因为您建议ID的值是正确的。我已经做了警报(ID),它是正确的。你能使用实时功能吗?嗨charlietfl
建议只需稍加修改即可生效。谢谢你的帮助,我已经把所有的东西都改成了课堂上的东西,仍然是同样的问题。。。我已经用更改更新了我的问题,因为您建议ID的值是正确的。我已经做了警报(ID),它是正确的。你能使用实时功能吗?嗨charlietfl
建议只需稍加修改即可生效。谢谢你的帮助非常感谢你的帮助。在调用$.ajax
之前,我创建了var publish=$(this)
,并在success:function(
中使用了publish.parent().html(
)函数()中使用了var publish=$(this)
,这是关于ajax请求中的this
的很好的一课..完成了吗!非常感谢您的帮助。我创建了var publish=$(这篇)
在调用$.ajax
之前,在success:function(
中使用了publish.parent().html(
)函数(),它工作得很好。关于ajax请求中的这个
的好教训..已经完成了!