我是 javascript 和 web 开发的新手,现在我正在尝试使用 codeigniter,我想在删除链接上设置一个 javascript 确认框。现在我使用它可以很好地工作:
<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 来让它正常工作。 任何帮助将不胜感激。
我最终使用了如下所示的 jquery 解决方案:
$(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。
最佳答案
试试这个...
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 确认弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12472478/