我正在尝试实现sweetalert2,但是由于某种原因,它不应用常规的html验证(即,required ='true'不起作用), 我没有收到该字段为空的错误。
没有swall2,它的确提示我该字段为空。
此外,一旦我在swall对话框中确认我要继续(“是,提交!”),它就不会提交表单。
我想念什么?
HTML:
<form>
<label>Last Name</label>
<input type="text" required="true"/>
<input class="btnn" type="submit" name="submit" id="btn-submit"/>
</form>
Javascript:
$('#btn-submit').on('click', function(e) {
e.preventDefault();
var form = $('form');
swal.fire({
title: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, submit it!",
closeOnConfirm: false
}, function(isConfirm) {
if (isConfirm) form.submit();
});
});
答案 0 :(得分:2)
文档说,迁移到sweetalert2后,结构已从回调方法更改
swal(
{title: 'Are you sure?', showCancelButton: true}, function(isConfirm) {
if (isConfirm) {
// handle confirm
} else {
// handle all other cases
}
}
)
基于承诺的方法
Swal.fire({title: 'Are you sure?', showCancelButton: true}).then(result => {
if (result.value) {
// handle Confirm button click
// result.value will contain `true` or the input value
} else {
// handle dismissals
// result.dismiss can be 'cancel', 'overlay', 'esc' or 'timer'
}
})
答案 1 :(得分:0)
您可以这样做。
$('#btn-submit').on('click',function(e){
e.preventDefault();
var form = $('form');
console.log("hello");
swal.fire({
title: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, submit it!",
}).then(function (result){
if(result.value === true){
console.log("Submitted");
form.submit();
}
});
});
有关更多信息,您可以从https://sweetalert2.github.io/#examples看有关SweetAlert2的示例
答案 2 :(得分:0)
您可以在提交表单时调用swal,当前单击“提交”按钮swal确认框即在表单提交之前调用。 JSFiddle链接:sweetalert 尝试以下代码:
$('#form1').on('submit',function(e){
e.preventDefault();
var form = $('form');
swal.fire({
title: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, submit it!",
closeOnConfirm: false
}, function(isConfirm){
if (isConfirm) form.submit();
});
});
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form name="form1" id="form1" method="post">
<label for="">Last Name</label><span class="required"> *</span>
<input type="text" required />
<input class="btnn" type="submit" name="submit" id="btn-submit"/>
</form>
</body>
</html>