У меня есть простая таблица с флажкой в одной ячейке таблицы - когда пользователь нажимает флажок, который он вызывает сценарий, который вызывает файл PHP, чтобы сделать некоторые обновления базы данных. Все это работает хорошо. Я думал, что следующее сделает это, но это внесет какие-либо изменения: < /p>
if (checkedState === 'true') {
$this.attr('checked', false);
} else {
$this.attr('checked', true);
}
< /code>
Я прикрепил полный сценарий, который работает, за исключением вышеуказанной части, где он пытается восстановить флажок к исходному проверенному состоянию. < /p>
< div class = "snippet">
$(document).ready(function() {
$("input.select-item").click(function() {
//console.log( 'starting Checklist Item Update' );
var recid = $(this).closest('td').attr('id');
var checkedState = $(this).is(":checked");
// Create a reference to $(this) here:
$this = $(this);
$.post('updateItem.php', {
type: 'updateItem',
recid: recid,
selectionType: checkedState
}, function(data) {
data = JSON.parse(data);
//console.log( data );
if (data.error) {
var ajaxError = (data.text);
var errorAlert = 'There was an error updating the Completed Checklist Item - ' + ajaxError + '. Please contact the Developer';
$this.closest('td').addClass("table-danger");
// Restore checkbox to it's original state
if (checkedState === 'true') {
$this.attr('checked', false);
} else {
$this.attr('checked', true);
}
//display AJAX error details
$("#updateSelectionsErrorMessage").html(errorAlert);
$("#updateSelectionsError").show();
return; // stop executing this function any further
} else {
$this.closest('td').addClass("table-success")
$this.closest('td').removeClass("table-danger");
// Restore checkbox to it's original state
if (checkedState == true) {
$this.attr('checked', false);
} else {
$this.attr('checked', true);
}
}
}).fail(function(xhr) {
var httpStatus = (xhr.status);
var ajaxError = 'There was an error updating the Completed Checklist Item - AJAX request error. HTTP Status: ' + httpStatus + '. Please contact the Developer';
$this.closest('td').addClass("table-danger");
//display AJAX error details
$("#updateSelectionsErrorMessage").html(ajaxError);
$("#updateSelectionsError").show();
$this.attr('checked', false); // Unchecks it
});
});
});< /code>
Category
Description
Completed
Check
Main
Lorem ipsum dolor sit amet
1
У меня есть простая таблица с флажкой в одной ячейке таблицы - когда пользователь нажимает флажок, который он вызывает сценарий, который вызывает файл PHP, чтобы сделать некоторые обновления базы данных. Все это работает хорошо. Я думал, что следующее сделает это, но это внесет какие-либо изменения: < /p> [code]if (checkedState === 'true') { $this.attr('checked', false); } else { $this.attr('checked', true); } < /code> Я прикрепил полный сценарий, который работает, за исключением вышеуказанной части, где он пытается восстановить флажок к исходному проверенному состоянию. < /p> < div class = "snippet">
$(document).ready(function() { $("input.select-item").click(function() { //console.log( 'starting Checklist Item Update' ); var recid = $(this).closest('td').attr('id'); var checkedState = $(this).is(":checked"); // Create a reference to $(this) here: $this = $(this); $.post('updateItem.php', { type: 'updateItem', recid: recid, selectionType: checkedState }, function(data) { data = JSON.parse(data); //console.log( data ); if (data.error) { var ajaxError = (data.text); var errorAlert = 'There was an error updating the Completed Checklist Item - ' + ajaxError + '. Please contact the Developer'; $this.closest('td').addClass("table-danger");
// Restore checkbox to it's original state if (checkedState === 'true') { $this.attr('checked', false); } else { $this.attr('checked', true); } //display AJAX error details $("#updateSelectionsErrorMessage").html(errorAlert); $("#updateSelectionsError").show(); return; // stop executing this function any further } else { $this.closest('td').addClass("table-success") $this.closest('td').removeClass("table-danger");
// Restore checkbox to it's original state if (checkedState == true) { $this.attr('checked', false); } else { $this.attr('checked', true); } } }).fail(function(xhr) { var httpStatus = (xhr.status); var ajaxError = 'There was an error updating the Completed Checklist Item - AJAX request error. HTTP Status: ' + httpStatus + '. Please contact the Developer'; $this.closest('td').addClass("table-danger"); //display AJAX error details $("#updateSelectionsErrorMessage").html(ajaxError); $("#updateSelectionsError").show(); $this.attr('checked', false); // Unchecks it }); }); });< /code>