我想在selectedColumnIndex
每次使用后將變數設定回 null 。
我需要能夠檢查selectedColumnIndex
每次使用后是否設定了新值。它可以連續使用一次或數十次(用于表格樣式 UI 組件中的“剪切和插入”功能)。
如果我在變數處放置瀏覽器斷點var check
,selectedColumnIndex
將顯示為null
. 但是,如果在沒有$(document).on('click', '.columnUpdatesInsert', function () { }
運行的情況下再次運行,將恢復到以前的值。 $(document).on('click', '.columnUpdates', function () { }
selectedColumnIndex
var selectedColumnIndex = null;
$(document).on('click', '.columnUpdates', function () {
selectedColumnIndex = $(this).attr("data-columnindex");
});
$(document).on('click', '.columnUpdatesInsert', function () {
if (selectedColumnIndex != null) {
// get variables from click element etc.
$(updateColumnPosition(tableId, selectedColumnIndex, newColumnIndex));
}
else {
alert("No column was selected to move.");
}
});
function updateColumnPosition(tableId, selectedColumnIndex, newColumnIndex) {
$.ajax({
type: "POST",
url: "/Task/UpdateColumnIndex",
data: { projectId: _projectId, tableId: tableId, selectedColumnIndex: selectedColumnIndex, newColumnIndex: newColumnIndex },
dataType: 'json',
success: function (data) {
if (data.success) {
// do other unrelated work
selectedColumnIndex = null; // this successfully sets it to null, but it is getting set back to the previous value before this code is explicitly setting it again.
var check = 0;
}
else {
// handle error
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status " " thrownError);
},
});
}
uj5u.com熱心網友回復:
可能是因為您將 selectedColumnIndex 作為 arg 傳遞給函式,它成為該函式的本地函式。
嘗試不要將值 selectedColumnIndex 作為 arg 傳遞,并始終將其用作全域。
var selectedColumnIndex = null;
$(document).on('click', '.columnUpdates', function () {
selectedColumnIndex = $(this).attr("data-columnindex");
});
$(document).on('click', '.columnUpdatesInsert', function () {
if (selectedColumnIndex != null) {
// get variables from click element etc.
$(updateColumnPosition(tableId, newColumnIndex));
}
else {
alert("No column was selected to move.");
}
});
function updateColumnPosition(tableId, newColumnIndex) {
$.ajax({
type: "POST",
url: "/Task/UpdateColumnIndex",
data: { projectId: _projectId, tableId: tableId, selectedColumnIndex: selectedColumnIndex, newColumnIndex: newColumnIndex },
dataType: 'json',
success: function (data) {
if (data.success) {
// do other unrelated work
selectedColumnIndex = null; // this successfully sets it to null, but it is getting set back to the previous value before this code is explicitly setting it again.
var check = 0;
}
else {
// handle error
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status " " thrownError);
},
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/508161.html
標籤:javascript jQuery
上一篇:沒有.NET3.5的MSAccess中的ArrayList?
下一篇:在JS檔案中運行PHP腳本