JavaScript确认弹出窗口 [英] Javascript confirmation popup
问题描述
我是javascript和Web开发的新手,现在我正在尝试进行codeigniter,我想在Delete链接上设置一个javascript确认框.现在,我可以使用它使它正常工作:
I am new to javascript and web development, now I am experimenting in codeigniter and I want to setup a javascript confirmation box on a delete link. Now I got it to work reasonably well using this:
<script type="text/javascript">
function con(message) {
var answer = confirm(message);
if (answer)
{
return true;
}
return false;
}
</script>
和这个:
echo '<a href="/groups/deletegroup/'.$group->id.'" OnClick="return con(\'Are you sure you want to delete the group?\');" class="btn small light-grey block">Delete</a>';
我遇到的问题是,第一次单击链接时没有弹出窗口,但是此后的每次单击都可以正常工作.我也觉得也许应该使用document.getElementById和window.onload使其正常工作. 任何帮助将不胜感激.
The problem I have is that the first time one clicks the link there is no popup, but every click after that works as it should. I also feel that maybe I should use document.getElementById and window.onload to get it to work properly. Any help would be greatly appreciated.
我最终使用了如下所示的jquery解决方案:
I eventually used a jquery solution looking like this:
$(document).ready(function (){
$('.delete').click(function (e) {
e.preventDefault();
var href = $(this).attr('href');
$.msgAlert({
title: 'Delete'
,text: 'Do you really want to delete this group?'
,type: 'error'
,callback: function () {
location.href = href;
}
});
})
});
我在msgAlert框中使用了商业化的msgUI.
I use the commercial msgUI for the msgAlert box.
推荐答案
尝试一下...
HTML:
<a href="#" id="btn-delete">Delete</a>
JS:
//window.onload = function() {
var btnDelete = document.getElementById('btn-delete');
btnDelete.onclick = function() {
var message = 'Are you sure you want to delete the group?';
if (confirm(message)) {
// delete something...
}
}
//};
这篇关于JavaScript确认弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!