javascript - 使用 jquery 5 秒后关闭对话框

标签 javascript jquery html twitter-bootstrap

我正在向我的 Rails 应用程序实现添加愿望 list 功能。到目前为止,我可以对话该消息并使其在 5 秒后消失。问题是,一旦淡出并且用户单击另一个“添加愿望 list ”,就不会出现对话框。他们必须刷新页面。我仍在学习,所以我知道发生了什么。我需要用 js 关闭对话框,但是如何关闭?

Coming from my last question :

HTML

<section data-id="notification"></section>

JavaScript

...

// Calling the notification();
// You need to reduce that data string!
notification("<div id='noti' class='alert alert-success'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><p>WishList Added!</p></div>");

...

function notification(data){
    $( "[data-id=notification]" ).append( data ).delay(5000).fadeOut(400); // I've tried adding .dialog('close')
    $('#noti').dialog('close'); // this gives an error.
  }

...

总而言之,当用户添加到愿望 list 时,将显示一个对话框。用户可以手动关闭对话框,否则它将在 x 秒后自行关闭。如果对话框自行关闭并且用户单击另一个添加愿望 list ,警报会重新出现等。我需要如何将其从 dom 中删除。

也许每次用户添加/删除愿望 list 时都显示一条消息是没有意义的,但这对于我的应用程序的其他部分很有用。

This question是4年前的事了。从那以后有什么改变吗?我需要两者:用户关闭和 js 关闭。

最佳答案

您应该使用alert('close'):

$( "[data-id=notification]" ).append( data ).delay(5000).fadeOut(400);
$('#noti').alert('close');

或者使用setTimeout:

$( "[data-id=notification]" ).append( data );
setTimeout(function() { $('#noti').alert('close'); }, 5000);

希望这有帮助。

关于javascript - 使用 jquery 5 秒后关闭对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38550779/

相关文章:

javascript - 如何为 "nested" Accordion 添加事件状态

jquery - 如何在特定页面上更改导航栏的颜色?

javascript - 将浏览器中加载的样式表合并并保存到一个文件中

python - 如何使用 ajax on key up 将此变量从 django View 发送到 html?

html - Razor View 表单中的多个提交按钮

javascript - Node memwatch : leak of type native

javascript循环遍历本地存储以查找元素

javascript - 为什么 mongoose 在创建和更新文档时异步运行?

javascript - 使用 jquery 获取总价

javascript - 我无法使用 rowReorder 替换 dataTable 中的行