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

时间:2017-10-21 06:41:27

标签: javascript jquery

如何在选中复选框并单击按钮后关闭弹出模式?

如果未选中复选框,则不关闭模态。复选框和按钮放在模态内。当我选中复选框并单击按钮时,模态未关闭。

$(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>

4 个答案:

答案 0 :(得分:0)

<!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>

答案 1 :(得分:0)

您可以创建一个功能,并将功能添加到按钮点击事件中。

<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');
}
}

答案 2 :(得分:0)

像这样更改关闭按钮代码

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

然后添加此JQuery代码

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

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

})

这里你去working sample

答案 3 :(得分:0)

        $("#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>

上面的代码对我有用