2016-05-01 13 views
1

ボタンをクリックしてアラートを表示し、そのボタンが閉じられるまでアラートを表示します。ブートストラップ警告が自動的に消えないようにするにはどうすればよいですか?

ボタンを押すたびに画面上に警告が点滅します。

通知が閉じられるまで表示されますが、ボタンを再度クリックしても表示されません。

http://jsfiddle.net/g1rnnr8r/108/

任意のアイデア?

HTML

<div class="form-group"> 
    <div class="col-lg-10 col-lg-offset-2"> 
    <button type="submit" class="btn btn-primary">Submit</button> 
    <div class="alert alert-dismissible alert-success"> 
     <button type="button" class="close" data-dismiss="alert">&times;</button> 
     <strong>Completed!</strong> Form successfully submitted... 
    </div> 
    </div> 
</div> 

CSS

<style> 
    .alert { 
     display: none; 
    } 
</style> 

JS

<script> 
    $(document).ready(function() { 
     $('button').click(function() { 
      $('.alert').show() 
     }) 
    }); 
</script> 

答えて

2

これを試してみてください。 See DEMO

data-dismiss="alert"を使用すると、alert divが完全に削除されます。だから、このようにしてみてください。

<body> 
    <button type="submit" class="btn btn-primary">Submit</button> 
    <div class="alert alert-dismissible alert-success"> 
     <button type="button" class="close">&times;</button> 
     <strong>Completed!</strong> Form successfully submitted... 
    </div> 
</body> 

JS:

$(document).ready(function(){ 
    $('button').click(function(){ 
     $('.alert').show() 
    }); 

    $('button').click(function(){ 
     $(this).parents('div').hide(); 
    }); 
}); 

CSS:

.alert{ 
    display: none; 
} 
+0

が、それはボタンがページを更新します私のWebフォームにいくつかの理由で、jsfiddleに適しています。 – girthquake

+0

@rks、送信ボタンがページをリフレッシュしている場合、 'event.preventDefault();' –

+0

を使用できます。 'event.preventDefault();'を使用してもページがリロードされません。私のページではなくjsfiddleで作業している理由がわからないのですが、私のウェブフォームに何か問題があるかもしれないと思います。 – girthquake

関連する問題