当提交按钮被按下时,我想给用户一个警告,确保他们想要删除一些东西。
到目前为止,我必须这样做
在 Twig 文件中
{{ form_start(form, {'attr': {'id': 'delete_form'}}) }}
在 Javascript 文件中
window.onload = function() {
confirmDelete();
};
function confirmDelete(){
var el = document.getElementById('delete_form');
if (el) {
el.addEventListener('submit', function () {
return confirm('Are you sure you want to delete this question?');
}, false);
}
else {
console.log("No form found");
}}
但现在,当点击警报的取消按钮时,数据仍在被删除。
我做错了什么?
最佳答案
您没有阻止提交表单。
// listen to the submit event
$('#delete_form').on('submit', function(e) {
// prevent form from being submitted
e.preventDefault();
confirmDelete();
});
function confirmDelete() {
var result = confirm('Are you sure you want to delete this question?');
// I do not know what result returns but in case that yes is true
if (result === true) {
$('#delete_form').submit();
}
}
关于javascript - 确认框 Twig、Symfony 和 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32565145/