简体   繁体   English

单击按钮时警报第二次不起作用

[英]Alert is not working second time when clicking on button

I am using bootstrap version so I have created a slide down menu same like below example.我使用的是引导程序版本,所以我创建了一个与下面示例相同的下拉菜单。

button not working second time按钮第二次不工作

<div class="alert alert-success" id="success-alert">
<button type="button" class="close" data-dismiss="alert">x</button>
<strong>Success! </strong>
Product have added to your wishlist.

The problem is that when I click on close button in slide down the bar is closing up.问题是,当我点击滑动中的关闭按钮时,栏正在关闭。

But when I again click on add wish list the slide down is not appearing again.但是当我再次点击添加心愿单时,幻灯片不会再次出现。

My problem is not only closing.我的问题不仅是关闭。 It should close when i click on 'x' button if not it should close automatically after specific time.当我点击“x”按钮时它应该关闭,否则它应该在特定时间后自动关闭。

Thanks.谢谢。

Remove this from your close button 'data-dismiss="alert"' and add one more click function like below:从关闭按钮 'data-dismiss="alert"' 中删除它,然后再添加一个点击功能,如下所示:

$(document).ready (function(){
    $("#success-alert").hide();
    $("#myWish").click(function showAlert() {
        $("#success-alert").fadeTo(2000, 500).slideUp(500, function(){
            $("#success-alert").slideUp(500);
        });   
    });

    $('#success-alert .close').click(function(){
        $(this).parent().hide();
    });
});

HTML will like: HTML 会喜欢:

<div class="product-options">
    <a  id="myWish" href="javascript:;"  class="btn btn-mini" >Add to Wishlist </a>
    <a  href="" class="btn btn-mini"> Purchase </a>
</div>
<div class="alert alert-success" id="success-alert">
    <button type="button" class="close">x</button>
    <strong>Success! </strong>
    Product have added to your wishlist.
</div>

This simple trick worked for me.这个简单的技巧对我有用。 Use this close.bs.alert bootsrap's event type to hide the element instead of removing it.使用close.bs.alert bootsrap 的事件类型来隐藏元素而不是删除它。

$('.alert').on('close.bs.alert', function (e) {
    e.preventDefault();
    $(this).addClass('hidden');
});

or或者

$('.alert').on('close.bs.alert', function (e) {
    e.preventDefault();
    $(this).hide();
});

Instead of writing whole code .I used slideUp() dealy() and slideDown() methods of Jquery.而不是写整个代码的。我使用的slideUp() dealy()slideDown()的jQuery方法。

 $(document).ready(function() { $("#success-alert").hide(); $("#btn2").click(function showAlert() { $("#success-alert").slideDown(300).delay(2000).slideUp(400); }); }); $('#success-alert .close').click(function() { $(this).parent().hide(); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="product-options"> <input type="button" id="btn2" value="Add to wish list" /> </div> <div class="alert alert-success" id="success-alert"> <button type="button" class="close" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> <strong>Success! </strong> Product have added to your wishlist. </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM