晚上好
我刚接触JavaScript。我有一本书,在寻找答案时会上网搜索。我看到JavaScript领域似乎有太多的路径和框架。对于这个问题,我正在尝试在Sweetalert中使用兼容的javascript。在选择选项之前,我在onclick完成功能时遇到问题。我将函数更改为默认情况下返回false,并让路径使onlick函数无效,然后单击按钮。以下是合理形式吗?还是会推荐另一种方法来达到相同的效果?如果是这样,请说明为什么更好。另外,请暂时没有框架/库的答案。
window.onload = function() {
function doSomething() {
var button = document.getElementById("edit");
swal({
title: 'Are you sure?',
text: 'You will not be able to recover this imaginary file!',
type: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, Update it!',
cancelButtonText: 'No, stop'
}).then((result) => {
if (result.value) {
button.onclick = null;
button.click();
// For more information about handling dismissals please visit
// https://sweetalert2.github.io/#handling-dismissals
} else if (result.dismiss === swal.DismissReason.cancel) {
swal(
'Cancelled',
'No Update Occured :)',
'error'
)
}
})
return false;
}
document.getElementById("edit").onclick = doSomething;
<form action="{{route('categories.update',$cat->id)}}" method="post">
{{method_field('patch')}}
<div class="form-group">
<label for="title">Name</label>
<input type="text" name="name" class="form-control" value="{{$cat->name}}">
<label for="description">Description</label>
<input type="text" name="description" class="form-control" value="{{$cat->description}}">
</div>
<button class="btn btn-primary" type="submit" id="edit">Edit Category</button>
{{@csrf_field()}}
</form>