简体   繁体   English

单击按钮后如何关闭弹出模式

[英]How to close popup modal after button click

How to close popup modal after the checkbox is checked and then the button is clicked?选中复选框然后单击按钮后如何关闭弹出模式?

If checkbox is not checked then not to close modal.如果未选中复选框,则不要关闭模态。 Checkbox and button placed inside modal.复选框和按钮放置在模态内。 When I checking the checkbox and clicking the button modal is not closing.当我选中复选框并单击按钮时,模态没有关闭。

 $(document).ready(function(){ $('.one:checked').on('click' , '.close_model' , function(){ dialog('close'); }); });
 <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Modal Example</h2> <!-- Trigger the modal with a button --> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> <!-- Modal --> <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Modal Header</h4> </div> <div class="modal-body"> <button type="button" class="close_model btn btn-danger">button</button> <input type="checkbox" name="ch" class="one"> <input type="checkbox" name="ch" class="one"> <input type="checkbox" name="ch" class="one"> <input type="checkbox" name="ch" class="one"> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> </div> </body> </html>

change close button code like this像这样更改关闭按钮代码

 <button type="button" class="btn btn-default close">Close</button>

and then add this JQuery code然后添加此 JQuery 代码

$('.close').click(function(){

if ($('.one').is(':checked'))
{
 $("#myModal").modal('toggle');
}

}) })

here you go working sample在这里你去工作样本

<!DOCTYPE html>
<html lang="en">
     <head>
            <title>Bootstrap Example</title>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
     </head>
     <body>
            <div class="container">
                 <h2>Modal Example</h2>
                 <!-- Trigger the modal with a button -->
                 <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
                 <!-- Modal -->
                 <div class="modal fade" id="myModal" role="dialog">
                        <div class="modal-dialog">
                             <!-- Modal content-->
                             <div class="modal-content">
                                    <div class="modal-header">
                                         <button type="button" class="close" data-dismiss="modal">&times;</button>
                                         <h4 class="modal-title">Modal Header</h4>
                                    </div>
                                    <div class="modal-body">
                                         <input type="hidden" name="txt1" id="textbox1">
                                         <button type="button" class="close_model btn btn-danger">button</button>
                                         <input type="checkbox" name="ch" class="one" value="1">
                                         <input type="checkbox" name="ch" class="one" value="2">
                                         <input type="checkbox" name="ch" class="one" value="3">
                                         <input type="checkbox" name="ch" class="one" value="4">
                                    </div>
                                    <div class="modal-footer">
                                         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                    </div>
                             </div>
                        </div>
                 </div>
            </div>
            <script type="text/javascript">
                 $(document).ready(function(){
                 $(".close_model").on("click",function(){
                 if($('#textbox1').val() == 'true'){
                    $('.close').trigger('click');
                 }else{
                    alert('pls click on checkbox');
                 }
                 });
                 $('.one').change(function() {
                            if(this.checked) {
                                    $(this).prop("checked", 'true');
                            }
                            $('#textbox1').val(this.checked);
                    });
                 });
            </script>
     </body>
</html>

Hi there create a function and ad the function to button click event.嗨,创建一个函数并将该函数广告到按钮单击事件。

<button type="button" class="close_model btn btn-danger" onclick="CheckChecked();">button</button>

Function CheckChecked()
{
var x = document.getElementById("ch").checked;
    if (x)
{
$('#myModal').modal('hide');
}
}
        $("#selectItem").click(function(){

        var value = $("input[type='radio']:checked").val();

        $('#town').val(value);
        $("#myModal").modal('hide'); 
    });

<button type="button" id="selectItem" name="selectItem" class="btn btn-primary">Select</button>

The above code worked for me上面的代码对我有用

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

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