简体   繁体   中英

Show Bootstrap alert box on a button click

The following code shows an alert box:

<head>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
        <h2>Dismissal Alert Messages</h2>
        <div class="alert alert-success alert-dismissable">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            Success! message sent successfully.
        </div>
    </div>
</body>

How can I show alert box on button click?

This jsfiddle shows how you can show a bootstrap alert box on click

http://jsfiddle.net/g1rnnr8r/2/

You need to implement jquery's show() method. The code you need to use is.

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

 function showAlert(){ if($("#myAlert").find("div#myAlert2").length==0){ $("#myAlert").append("<div class='alert alert-success alert-dismissable' id='myAlert2'> <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button> Success! message sent successfully.</div>"); } $("#myAlert").css("display", ""); }
 <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> <button value="showAlert" onclick="showAlert();"> Show Alert</button> <div class="container" style="display:none;" id="myAlert"> <h2>Dismissal Alert Messages</h2> <div class="alert alert-success alert-dismissable" id="myAlert2"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> Success! message sent successfully. </div> </div>
Edited

I've added some IDs and written some code. Try to understand, If you are not getting ask me. Okay Hope this will help for you, If not ask me for more.

Try This. What is done here is:

  1. Added one button "Show Alert message".
  2. By Default alert message will be hidden.
  3. On click of "Show alert message" alert message will be shown

 $("#btnShow").click(function(){ $(".alert").hide().show('medium'); });
 .alert{ display:none; }
 <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Dismissal Alert Messages</h2> <button id="btnShow">Show Alert message</button> <div class="alert alert-success alert-dismissable"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button> Success! message sent successfully. </div> </div> </body>

You can check this one:

 <div class="container">
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
        <div class="alert alert-success alert-dismissible">
            <a  class="close" data-dismiss="modal" aria-label="close">&times;</a>
            <strong>Success!</strong> Indicates a successful or positive action.
          </div>
    </div>
  </div>
</div>

If you want to create custom modal dialog box, then you could use this small library for it: http://bootboxjs.com/

<!-- bootbox code -->
    <script src="bootbox.min.js"></script>
    <script>
        $(document).on("click", ".alert", function(e) {
            bootbox.alert("Hello world!", function() {
                console.log("Alert Callback");
            });
        });
    </script>

Otherwise you need to create the modal and javascript triggers manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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