我有一个简单的表格,其中一个单元格中有一个复选框 - 当用户单击复选框时,它会调用一个脚本,该脚本会调用 php 文件来更新数据库。一切运行良好。
如果 php 脚本出现错误或返回错误,我想将复选框恢复到用户单击之前的原始选中状态。我以为下面的代码可以做到这一点,但它没有做出任何改变:
if (checkedState === 'true') {
$this.attr('checked', false);
} else {
$this.attr('checked', true);
}
我已附加完整的脚本,除了上面尝试将复选框恢复到其原始选中状态的部分外,该脚本均可正常工作。
$(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
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<table class="table table-striped table-hover table-bordered">
<thead>
<th style="width: 20%" scope="col">Category</th>
<th style="width: 60%" scope="col">Description</th>
<th style="width: 10%" scope="col">Completed</th>
<th style="width: 10%" scope="col">Check</th>
</thead>
<tbody>
<tr id="1">
<td>Main</td>
<td>Lorem ipsum dolor sit amet</td>
<td>1</td>
<td id="1"><input type="checkbox" class="select-item checkbox" name="select-item" value="1"></td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function () {
$("input.select-item").click(function () {
var recid = $(this).closest("td").attr("id");
var checkedState = $(this).is(":checked"); // Ini boolean (true/false)
var $this = $(this);
$.post("updateItem.php", {
type: "updateItem",
recid: recid,
selectionType: checkedState,
})
.done(function (data) {
data = JSON.parse(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 ke kondisi sebelumnya
$this.prop("checked", !checkedState);
// Tampilkan pesan error
$("#updateSelectionsErrorMessage").html(errorAlert);
$("#updateSelectionsError").show();
} else {
$this.closest("td").addClass("table-success");
$this.closest("td").removeClass("table-danger");
}
})
.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");
// Tampilkan pesan error
$("#updateSelectionsErrorMessage").html(ajaxError);
$("#updateSelectionsError").show();
// Restore checkbox ke kondisi sebelumnya
$this.prop("checked", !checkedState);
});
});
});
</script>
下载整个项目。zip在这里 此问题的代码在Views文件夹中的CreateSchedule.aspx文件中。 以下是GridView的ASP.NET: 在取消选中某个框后调试事件时,网页上的复选框显示为未选中,但的值为true,即使该复选框在网页上显示为未选中。(chkRow是未选中的复选框) 更新3 我取得了一些突破。我发现复选框的问题在于嵌套的gridview位于使用JavaScript折叠和
我现在被困在这个问题上。我想如果复选框被选中,它应该返回一个值,但是当您将public更改为int时,您会得到一个错误。它似乎只对void有效,void不会返回任何东西。有办法做到这一点吗?
当前表格每一行由表单生成。且表格是通过jquery 的dataTable生成,第一列为复选框,在表格中追加行时如何同时为每一行添加勾选框啊。这个是我加的,但显然是有问题的 以下是相关代码 这里是添加每一行的代码
我尝试用docker运行一个简单的java类,执行以下步骤: > 创建java文件(helloworld.java) 编译它(javac helloworld.java) 创建dockerfile(位于dockerfile内容的上方) 然后我使用以下命令构建它: 这是我机器上的java版本: 顺便说一句,我在我的机器上用相同的java版本构建我的java文件,因此我不知道为什么错误说,HelloW
当html页面上没有任何变量可以切换时,是否有人知道如何使用selenium处理或读出复选框的状态(true/false)。我已经尝试了常用函数。isselected()和。isenabled(),但这些函数只读取不可用的值。 有人知道有没有办法得到这个身份吗?此外,xml的屏幕截图以及它是哪个元素。 如果有人有主意就太好了。我在用Java编程。
我有一个有82,535行的表,其中65,087行按ID是唯一的。当我拉取整个结果集82,535并复制到Excel并删除重复项时,它显示有17,448个重复项。但是当我使用下面的查询时,我得到了不同的结果: 此查询返回值17364 我确定唯一的的数目是65,087
我正在尝试如何暂停一首确定的歌曲,存储它的内容,这样就可以加载另一首歌曲,播放新歌曲,恢复第一首歌曲中保存的内容并继续它。 有更好的办法吗? 谢了。
问题内容: 我有一组复选框,这些复选框在选中时将值传递为1,否则将值传递为0。但是,不是将未选中的复选框的值发送为“ 0”,而是发送的值为“ NULL”。 我在下面的JS代码中应将值相应地设置为0/1,但是仍然将该值作为NULL发送。有什么方法可以确保在未选中复选框的情况下传递的值为0? 更新 这是我的html: 默认情况下,它们都被选中。 如果未选中其中之一,则MySQL报告以下错误:0SQLS