我目前正在研究用于搜索、删除和编辑字段的表格示例的代码,我想使用 Sweetalert 使 javascript 警报更具吸引力,虽然我已经设法让它们显示在我的 javascript 代码中,但我无法让他们删除该字段,现在是的,我使用 confirmDel 删除功能没有任何问题。
javascript代码应该以什么方式消除甜蜜警报?
var script = document.createElement('script');
script.src = "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.10.1/sweetalert2.all.min.js";
script.src = "https://cdnjs.cloudflare.com/ajax/libs/sweetalert/0.4.2/sweet-alert.min.js";
script.src = "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.10.1/sweetalert2.all.min.js";
document.getElementsByTagName('script')[0].parentNode.appendChild(script);
//for CSS file
$('head').append('<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/0.4.2/sweet-alert.css" />');
function lista_libros(valor){
$.ajax({
url:'../Controllers/libros.php',
type:'POST',
data:'valor='+valor+'&boton=buscar'
}).done(function(resp){
//alert(resp);
var valores = eval(resp);
html="<table class='rwd-table'><thead><tr><th>#</th><th>ID Solicitud</th><th>Usuario</th><th>Miniauditorio</th><th>Equipo</th><th>Fecha-Solicitud</th><th>Fecha-Evento</th><th>Evento-Culmina</th><th>Hora-Inicio-Evento</th><th>Hora-Fin-Evento</th><th>Detalles</th><th>Estatus</th></tr></thead><tbody>";
for(i=0;i<valores.length;i++){
datos=valores[i][0]+"*"+valores[i][0]+"*"+valores[i][2]+"*"+valores[i][3]+"*"+valores[i][4]+"*"+valores[i][5]+"*"+valores[i][6]+"*"+valores[i][7]+"*"+valores[i][8]+"*"+valores[i][9]+"*"+valores[i][10]+"*"+valores[i][11];
html+="<tr><td>"+(i+1)+"</td><td>"+valores[i][0]+"</td><td>"+valores[i][1]+"</td><td>"+valores[i][2]+"</td><td>"+valores[i][4]+"</td><td>"+valores[i][5]+"</td><td>"+valores[i][6]+"</td><td>"+valores[i][7]+"</td><td>"+valores[i][8]+"</td><td>"+valores[i][9]+"</td><td>"+valores[i][10]+"</td><td>"+valores[i][11]+"</td><td><button class='btn btn-success' data-toggle='modal' data-target='#modallibros' onclick='mostrar("+'"'+datos+'"'+");'><span class='glyphicon glyphicon-pencil'></span></button><button class='btn btn-danger' onclick='sweetDelete("+'"'+valores[i][0]+'"'+")'><span class='glyphicon glyphicon-remove'></span></button></td></tr>";
}
html+="</tbody></table>"
$("#lista").html(html);
});
}
function guardar(){
var datosform=$("#formLibro").serialize();
$.ajax({
url:'../Controllers/libros.php',
type:'POST',
data:datosform+"&boton=actualizar"
}).done(function(resp){
if(resp==='exito'){
$('#exito').show();
lista_libros('');
}
else{
alert(resp);
}
});
}
function mostrar(datos){
//alert(datos);
var d=datos.split("*");
//alert(d.length);
$("#idusuario").val(d[0]);
$("#miniauditorio").val(d[2]);
$("#equipo").val(d[4]);
$("#fechaevento").val(d[6]);
$("#eventoculmina").val(d[7]);
$("#horainicio").val(d[8]);
$("#horafin").val(d[9]);
$("#status").val(d[11]);
$("#detalles").val(d[10]);
}
function eliminar(id){
//alert(id);
$.ajax({
url:'../Controllers/libros.php',
type:'POST',
data:'idusuario='+id+'&boton=eliminar'
}).done(function(resp){
alert(resp);
lista_libros('');
});
}
//
//
/*
function confirmDel(id)
{
var agree=confirm("¿Realmente desea eliminarlo? Los datos se eliminarán permanentemente");
if (agree==true){
eliminar(id);
}else{
return false;
}
}
*/
function eliminar(id){
$.ajax({
url:'../Controllers/libros.php',
type:'POST',
data:'idusuario='+id+'&boton=eliminar'
}).done(function(resp){
alert(resp);
lista_libros('');
});
}
/*sweet alert*/
function sweetDelete(id){
swal({
title: '¿Estás Seguro?',
text: 'No podrás recuperar esta información!',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Sí, eliminalo!',
cancelButtonText: 'Cancelar!',
closeOnConfirm: false
},
function(){
swal(
'Deleted!',
'Your file has been deleted.',
'success'
);
});
}
/* Tratado de realizar una confirmacion para eliminar, hace la pregunta pero al confirmar no elimina
function confirmDel()
{
var agree=confirm("¿Realmente desea eliminarlo? ");
if (agree) function eliminar("+'"'+valores[i][0]+'"'+");
return false;
}
*/
如果您使用的是最新版本的 SweetAlert,您应该使用 promise 语法,并简单地将调用添加到删除代码或删除函数
then()
: