我有以下jQuery代碼,它不工作,因爲我期望它。我確信我做錯了什麼,但我沒有看到它。基本上,如果博客不發佈,當我點擊圖片時,它訪問我的數據庫並相應地更新它。所有這一切都很好。jQuery多個值更新問題
- 我點擊.not_published
- .not_published與圖像標記,顯示Ajax的loader.gif
- 當服務器返回一個成功消息改變的內容,我更新#not_published的HTML父母的新內容包括使用.published替換.not_published。
我得到的,當我點擊.not_published是AJAX圖像加載,並在那裏停留。如果我在成功函數內添加一個alert("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);
}
});
});
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>
你爲什麼要通過AJAX加載圖像注意到並沒有直接加載,只是更改源?還有爲什麼你應該改變ID,當你可以改變具有相同效果的類時。該ID「必須」是唯一的才能正常工作。嘗試HADI的解決方案 – Vogel612
ID必須是唯一的。使用類 – Popnoodles