javascript - 当sweetalert弹出消息显示时如何使复选框值不改变
问题描述
我有一个具有基本值的复选框。我想让用户可以通过单击复选框中的选项来更改值,然后显示警报消息以确保他们想要更改它。如果是,则复选框值正在更改,但如果不是,则复选框值不会更改。
我已经尝试使用sweetalert
插件构建它。现在的问题是,当用户单击复选框值更改值,然后显示sweetalert
弹出窗口时,复选框值已经更改。因此,即使在模式中单击了用户no
选项,我也需要重新加载页面。sweetalert
如何使checkbox
值仅在用户单击模式yes
中的选项后更改sweetalert
?
这是现在的视图代码
<div class="col-lg-6">
<!--begin::Card-->
<div class="card card-custom gutter-b example example-compact">
<div class="card-header">
<h3 class="card-title">General</h3>
</div>
<div class="card-body" style="height:80px">
<div class="checkbox-inline">
<label class="checkbox">
<input type="checkbox" name="Checkboxes2" id="checkbox1" onclick='editGeneral(this.value)'/>
<span></span>
Checkin Status
</label>
</div>
</div>
</div>
<!--end::Card-->
</div>
这是ajax代码
function editGeneral(){
Swal.fire({
text: "Are you sure to change this ?",
icon: "warning",
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes!',
cancelButtonText: 'No'
}).then((result) => {
if(result.isConfirmed) {
var tes = $("#checkbox1").is(":checked");
$.ajax({
url: "<?php echo base_url(); ?>contoller/changefunction",
type:"POST",
dataType:"json",
data:{"config_value":tes},
success: function(response){
Swal.fire({
title: 'Success',
text: 'Status has been changed',
icon: 'success',
timer: 2000,
showCancelButton: false,
showConfirmationButton: false,
}).then((hasil) => {
location.reload();
});
}
})
}else{
Swal.fire({
timer: 10,
showCancelButton: false,
showConfirmationButton: false,
}).then((hasil) => {
location.reload();
});
}
});
}
谢谢
解决方案
我已经修改了您的代码以使其在这里工作,您可以看到我将所有复选框传递为onclick='editGeneral(this)'
,所以当甜蜜通知被确认或未确认时,我将根据您的选择更改复选框。
function editGeneral(el){
Swal.fire({
text: "Are you sure to change this ?",
icon: "warning",
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes!',
cancelButtonText: 'No'
}).then((result) => {
if(result.isConfirmed) {
(el.checked) ? el.checked = true : el.checked = false;
/*
$.ajax({
url: "<?php echo base_url(); ?>contoller/changefunction",
type:"POST",
dataType:"json",
data:{"config_value":tes},
success: function(response){
Swal.fire({
title: 'Success',
text: 'Status has been changed',
icon: 'success',
timer: 2000,
showCancelButton: false,
showConfirmationButton: false,
}).then((hasil) => {
location.reload();
});
}
})
*/
}else{
(el.checked) ? el.checked = false : el.checked = true;
Swal.fire({
timer: 10,
showCancelButton: false,
showConfirmButton: false,
}).then((hasil) => {
//location.reload();
});
}
});
}
<div class="col-lg-6">
<!--begin::Card-->
<div class="card card-custom gutter-b example example-compact">
<div class="card-header">
<h3 class="card-title">General</h3>
</div>
<div class="card-body" style="height:80px">
<div class="checkbox-inline">
<label class="checkbox">
<input type="checkbox" name="Checkboxes2" id="checkbox1" onclick='editGeneral(this)' />
<span></span>
Checkin Status
</label>
</div>
</div>
</div>
<!--end::Card-->
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/sweetalert2@10"></script>
另一个建议命令showConfirmationButton
是showConfirmButton
推荐阅读
- python - 命令行生成的文件不存在
- c# - 使用 odbc 驱动程序进行慢速查询
- sql - LockAmountOnline_Active:聚合可能不会出现在 WHERE 子句中,除非它在子查询中
- c# - 无法通过 Google Docs API 删除表中的行
- ibm-watson - 我无法完成 watson studio 设置,我不断收到错误消息“无法读取属性 'language_info' 未定义
- php - 在外部 for 循环的第一次迭代之后,内部 for 循环不起作用
- mysql - Mysql 长进程列表和自动重启
- azure-devops - Azure Dev Ops:撤消其他用户的签出
- reactjs - 如何显示道具并在视图上从组件渲染到另一个?
- android - Android dataBinding不刷新ui