我試圖更新從ajax調用返回的數據,但我收到一個錯誤框內容(與盒內容的類最接近的div)。JavaScript onclick .closest給出錯誤?
.box-content是頁面上5個div的類,但不應該最近能找到最接近的一個嗎?如果有意義的話,每個盒子都有一個盒子內容類及其父代的父代?
HTML:
<div style="margin-top:12%;" id="removeButton" class="btn btn-danger btn-sm" onclick="fireGovernmentMember({{ $playerRp->user_id }}, this);"><i class="fa fa-times"></i> Remove</div>
的Javascript:
function fireGovernmentMember(playerId, element) {
$.ajax({
url: '/' + 'api/ajax/owner/fire_gov',
type: "GET",
data: {
player_id: playerId,
},
statusCode: {
400: function (response) {
showErrorNotification("Something went wrong", response.responseText, 3000);
},
500: function (response) {
showErrorNotification("Something went wrong", response.responseText, 3000);
}
},
success: function(data) {
element.closest('.box-content').html(data);
showSuccessNotification("Action Completed", "Item has been removed from user.", 1000);
},
});
}
錯誤:
Uncaught TypeError: Cannot read property 'html' of null
at Object.success (override.js?v=1502133651:806)
at i (jquery-3.1.1.min.js:2)
at Object.fireWith [as resolveWith] (jquery-3.1.1.min.js:2)
at A (jquery-3.1.1.min.js:4)
at XMLHttpRequest.<anonymous> (jquery-3.1.1.min.js:4)
在哪裏,'element'哪裏來的?東西告訴我這是一個本地DOM節點,而不是一個jQuery元素 – adeneo
您正在傳遞'this'到內聯onclick上。它不會是一個jQuery對象。將它包裝在$()中,使其成爲訪問最接近的對象() – Taplar
,因爲元素不是jQuery – epascarello