gpt4 book ai didi

Javascript 确认弹出窗口

转载 作者:行者123 更新时间:2023-11-30 06:54:29 25 4
gpt4 key购买 nike

我是 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...
}
}
//};

http://jsfiddle.net/PEHx9/

关于Javascript 确认弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12472478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com