晚上好, 我是开发JS的初学者,在尝试删除项目时,我曾多次尝试向用户发出confilamation(甜蜜警报)。
我使用任何事件来触发警报?
这是我在Html中的代码:
<!-- sweet alerts Css-->
<link href="{{ asset('template/assets/sweet-alert/sweet-alert.min.css') }}" rel="stylesheet">
<!-- sweet alert JS-->
<script src="{{ asset('template/assets/sweet-alert/sweet-alert.min.js') }}"></script>
<script src="{{ asset('template/assets/sweet-alert/sweet-alert.init.js') }}"></script>
<!-- form--->
<form id="deleteAll" method="post" action="{{ path('projets_deleteAll') }}"
<!--checkAll --->
<input type="checkbox" id="checkAll"/>
{% for entity in entities %}
<input type="checkbox" id="d" name="check[]" value="{{ entity.id}}"/>
{% endfor %}
<strong >
<button type="submit" class="btn btn-lg btn-danger" id="bn" >
<i class="fa fa-trash" ></i> Delete</button>
</strong>
</form>
<!-- ./form--->
-Code Jquery。
$(document).ready(function(){
$("#checkAll").click(function(){
var checked_status = this.checked;
$("input[name='check[]']").each(function(){
this.checked = checked_status;
var v = $("#d").val();
$('#bn').trigger('submit', function (e){
e.preventDefault();
actiondesubsitution();
});
});
});
if(v.length>0){
document.querySelector('#d').onclick = function(){
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Yes, delete it!',
closeOnConfirm: false
},
function(){
swal("Deleted!", "Your imaginary file has been deleted!", "success");
});
};
}
else{
new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
.setTitleText("Oops...")
.setContentText("No selection for deleting !")
.show();
}
});
警告在“如果”
时无效的问题
错误是:
VM1631:187未捕获的DOMException:无法执行'querySelector' 'Document':'[object HTMLDocument]'不是有效的选择器。 在错误(本机) 在CommandLineAPIImpl。$(&lt; anonymous&gt;:187:84) 在&lt; anonymous&gt;:1:1CommandLineAPIImpl。$ @ VM1631:187(匿名函数)@ VM1633:1 javascript: 的console.log(v.length); VM1634:1未捕获的ReferenceError:v不是 定义 at&lt; anonymous&gt ;:1:25(匿名函数)@ VM1634:1
感谢您帮我整合此提醒。
答案 0 :(得分:1)
我假设您包含<script src="dist/sweetalert.min.js"></script>
和<link rel="stylesheet" type="text/css" href="dist/sweetalert.css">
标记,如果您这样做,您只需将第二个代码放在链接到该事件的函数中,就像您可以看到按F12一样在演示页面上,最后检查脚本标签上的代码:
document.querySelector('ul.examples li.warning.confirm button').onclick = function(){
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Yes, delete it!',
closeOnConfirm: false
},
function(){
swal("Deleted!", "Your imaginary file has been deleted!", "success");
});
};
尝试:
$(document).on("click", "#bn", function(){
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Yes, delete it!',
closeOnConfirm: false
},
function(){
swal("Deleted!", "Your imaginary file has been deleted!", "success");
});
});
我无法测试它,但这会显示你的消息...但我不确定它将如何链接删除元素功能......
代码应如下所示:
$(document).on("click", "#bn", function(){
if(v.length>0){
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Yes, delete it!',
closeOnConfirm: false
},
function(){
swal("Deleted!", "Your imaginary file has been deleted!", "success");
});
}
else{
new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
.setTitleText("Oops...")
.setContentText("No selection for deleting !")
.show();
}
});
是吗?
应该在事件内部评估条件,因为此时您的v
应该有值......