我有一个引导模式对话框,我想首先显示,然后当用户在页面上单击时,它就消失了。我有以下几点:

$(function () {
   $('#modal').modal(toggle)
});

 <div class="modal" id='modal'>
     <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">Error:</h3>
        </div>
        <div class="modal-body">
        <p>Please correct the following errors:</p>
        </div>
     </div>
 </div>

模态最初会显示,但在模态之外单击时不会关闭。此外,内容区域没有灰色。我怎么能得到模态显示最初,然后关闭后,用户点击区域外?我怎么能得到背景是灰色的演示?


当前回答

使用模态。Hide只会隐藏模态。如果你在模态下面使用覆盖,它仍然会在那里。使用点击调用实际关闭模式,并删除覆盖。

$("#modalID .close").click()

其他回答

   function Delete(){
       var id = $("#dgetid").val();
       debugger
       $.ajax({
           type: "POST",
           url: "Add_NewProduct.aspx/DeleteData",
           data: "{id:'" + id + "'}",
           datatype: "json",
           contentType: "application/json; charset=utf-8",
           success: function (result) {
               if (result.d == 1) {
                   bindData();
                   setTimeout(function(){ $('#DeleteModal').modal('hide');},1000);
               }
           }
       });
   };

这很有效

$(function () {
   $('#modal').modal('toggle');
});

然而,当你有多个情态动词堆叠在另一个上面时,它是无效的,所以,相反,这是有效的

data-dismiss="modal"

试试这个

$('#modal_id').modal('hide');

用modal('toggle')代替modal(toggle)

$(function () {
   $('#modal').modal('toggle');
});

在我的例子中,我使用了一个按钮来显示模式

<button type="button" class="btn btn-success" style="color:white"
    data-toggle="modal" data-target="#my-modal-to-show" >
    <i class="fas fa-plus"></i> Call MODAL
</button>

因此,在我的代码中,为了关闭模态(id = 'my-modal-to-show'),我调用了这个函数(在Angular typescript中):

closeById(modalId: string) {
  $(modalId).modal('toggle');
  $(modalId+ ' .close').click();
}

如果我调用$(modalId).modal('hide')它不工作,我不知道为什么

PS:在我的模态中,我用.close类编写了按钮元素

<button type="button" class="close" data-dismiss="modal" aria-label="Close"> 
    <span aria-hidden="true">&times;</span>
</button>