ChatGPT解决这个技术问题 Extra ChatGPT

我有一个我想最初显示的引导模式对话框,然后当用户点击页面时,它就会消失。我有以下内容:

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

模态框最初显示,但在模态框外单击时不会关闭。此外,内容区域没有变灰。我怎样才能让模态最初显示,然后在用户在区域外单击后关闭?我怎样才能让背景像演示中那样变灰?

您是使用 $("#yourModal").modal() 还是 $('.modal').modal() 初始化您的模式?

T
Tamil Selvan C

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

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

完全包括“切换”是多余的。只要确保在模态 div 上没有“隐藏”类。但是,是的,错字导致了这个问题。所以+1
不,它没有按预期工作,它隐藏了模式元素但背景覆盖元素仍然存在,您应该使用@Subodth 解决方案。
@Pierre - 考虑删除你的评论,.modal().hide() 与 .modal('hide') 不同,你会混淆人们。
就像帕里克所说,正确的答案是使用 modal('hide')
如果模态在另一个模态上,这是行不通的。谢谢。
S
Subodh Ghulaxe

要关闭引导程序 modal,您可以将 'hide' 作为选项传递给模态方法,如下所示

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

请查看工作fiddle here

bootstrap 还提供了可以与 modal 功能挂钩的事件,例如,如果您想在模式对用户隐藏完毕后触发事件,您可以使用 hidden.bs.modal 事件在 Documentation 中阅读有关模态方法和事件的更多信息

如果上述方法都不起作用,请为您的关闭按钮提供一个 ID 并触发单击关闭按钮。


$('#modal').modal('toggle');更好地隐藏模态阴影
@hamzeh.hanandeh,toggle 保留覆盖,不是解决方案。您应该始终使用 showhide
R
Robert Benyi
$('#modal').modal('toggle'); 

或者

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

应该管用。

但如果没有其他工作,您可以直接调用模态关闭按钮:

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

带有 hide() 的那个会关闭模式,但会使背景模糊。 $("#modal .close").click() 做得很好。谢谢!
这已经清楚地记录在案了,这里不需要伪造点击,这看起来很笨拙。正确答案是:$('#modal').modal('hide');
这 -> $('#modal').modal().hide();
我有一个不使用 $('#modal').modal('hide') 关闭的模型,但是我可以使用 $('#modal').modal('toggle') 关闭它,但在关闭后会显示一个垂直滚动条。所以对我来说 $('#modal').hide() 工作得很好,但我想知道这是否会产生任何问题?我在 $('#modal .close').click() 内编码,所以我认为我不能用它来关闭模式。
A
AmmarCSE

这对我有用:

<span class="button" data-dismiss="modal" aria-label="Close">cancel</span>

使用此链接 modal close


L
Love Kumar

尝试这个

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

我认为这提供了问题的答案。
我同意。这个答案更准确地回答了这个问题。
@MarceloAgimóvel :-)
Y
Yishai Landau
$("#your-modal-id").modal('hide');

通过类 ($(".my-modal")) 运行此调用将不起作用。


u
user3869304

这个非常好,它也适用于 angular 2

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

对我有用,而不是无效的 $('#modal').modal('toggle');
S
Sajad Mirzaei

您可以看到 this reference,但如果此链接已被删除,请阅读以下说明:

使用单行 JavaScript 调用 id 为 myModal 的模式:

$('#myModal').modal(options)

选项

选项可以通过数据属性或 JavaScript 传递。对于数据属性,将选项名称附加到 data-,如 data-backdrop=""。

|-----------|-------------|--------|---------------------------------------------|
| Name      | Type        | Default| Description                                 |
|-----------|-------------|--------|---------------------------------------------|
| backdrop  | boolean or  | true   | Includes a modal-backdrop element.          |
|           | the string  |        | Alternatively, specify static for a         |
|           | 'static'    |        | backdrop which doesn't close the modal      |
|           |             |        | on click.                                   |
|           |             |        |                                             |
| keyboard  | boolean     | true   | Closes the modal when escape key is pressed.|   
|           |             |        |                                             |
| focus     | boolean     | true   | Puts the focus on the modal when initialized|       
|           |             |        |                                             |
| show      | boolean     | true   | Shows the modal when initialized.           |
|-----------|-------------|--------|---------------------------------------------|

方法

异步方法和转换 所有 API 方法都是异步的并启动转换。他们在转换开始但在结束之前立即返回给调用者。此外,过渡组件上的方法调用将被忽略。有关更多信息,请参阅我们的 JavaScript 文档。

.modal(选项)

将您的内容激活为模式。接受一个可选的选项对象。

$('#myModal').modal({
   keyboard: false
})

.modal('切换')

手动切换模式。在模态实际显示或隐藏之前(即在显示.bs.modal 或 hidden.bs.modal 事件发生之前)返回给调用者。

$('#myModal').modal('toggle')

.modal('显示')

手动打开一个模态。在模态实际显示之前(即在 shown.bs.modal 事件发生之前)返回给调用者。

$('#myModal').modal('show')

.modal('隐藏')

手动隐藏模态。在模态实际上被隐藏之前(即在 hidden.bs.modal 事件发生之前)返回给调用者。

$('#myModal').modal('hide')

.modal('handleUpdate')

如果模态在打开时的高度发生变化(即出现滚动条时),手动重新调整模态的位置。

$('#myModal').modal('handleUpdate')

.modal('处置')

销毁元素的模态。

活动

Bootstrap 的模态类公开了一些用于连接模态功能的事件。所有模态事件都在模态本身触发(即在 ** 处)。类型 描述

|----------------|--------------------------------------------------------------|
| Event Type     | Description                                                  |
|----------------|--------------------------------------------------------------|
| show.bs.modal  | This event fires immediately when the **show** instance      |
|                | method is called. If caused by a click, the clicked element  |
|                | is available as the **relatedTarget** property of the event. | 
|                |                                                              |
| shown.bs.modal | This event is fired when the modal has been made visible to  |
|                | the user (will wait for CSS transitions to complete). If     |
|                | caused by a click, the clicked element is available as the   | 
|                | **relatedTarget** property of the event.                     |
|                |                                                              |
| hide.bs.modal  | This event is fired immediately when the **hide** instance   |
|                | method has been called.                                      |
|                |                                                              |
| hidden.bs.modal| This event is fired when the modal has finished being hidden |
|                | from the user (will wait for CSS transitions to complete).   |
|----------------|--------------------------------------------------------------|

$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})

A
Andre Van Zuydam

我在这个上的五分钱是我不想用一个 id 来定位引导模式,并且看到一次应该只有一个模式,以下应该足以移除模式,因为切换可能是危险的:

$('.modal').removeClass('show');

意图是好的,但这种方法并不总是有效。其他页面元素(包括 <body>)添加了类,这些类也必须还原。最好的答案是使用 'hide' 方法。
您可以为元素定义 id,但问题是模糊背景保持不变。我用这个 $('.modal-backdrop').remove() 解决了
z
zinczinc

在某些情况下,打字错误可能是罪魁祸首。例如,确保您有:

data-dismiss="modal"

并不是

data-dissmiss="modal"

请注意第二个示例中的双“ss”将导致关闭按钮失败。


S
Sheo Dayal Singh

我们可以通过以下方式关闭模态弹窗:

// We use data-dismiss property of modal-up in html to close the modal-up,such as

<div class='modal-footer'><button type='button' class="btn btn-default" data-dismiss='modal'>Close</button></div>

 // We can close the modal pop-up through java script, such as

 <div class='modal fade pageModal'  id='openModal' tabindex='-1' data-keyboard='false' data-backdrop='static'>

    $('#openModal').modal('hide'); //Using modal pop-up Id.
    $('.pageModal').modal('hide'); //Using class that is defined in modal html.

V
Vini.g.fer

$('.modal.in').modal('hide');

如果您在一页中使用多个模态弹出窗口,请使用上面的代码隐藏模态的背景。


或者只有 $('.modal').modal('hide'); 可以。
G
Ganesh Putta

引导示例

Modal Example


错误:“消息”:“未捕获的 TypeError:$(...).modal 不是函数”
是的。如果 Chrome(Linux Mint)中的“运行代码片段”出现错误。但它适用于 Firefox。
@Ivan Burlutskiy,感谢您通知我,这是 jquery 包含的问题,所以我修复了它。现在它在所有浏览器中都能正常工作。
R
Rup
   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);
               }
           }
       });
   };

嗨 - 感谢您的回答。我已将其格式化为代码(这相当简单 - 只需将第一行缩进一点)。但是我猜这里只有 $('#DeleteModal').modal('hide'); 是相关的?
Z
Zohab Ali

我用这个技巧以编程方式关闭了模态

使用 data-dismiss="modal" 添加模态按钮并使用 display: none 隐藏按钮。这是它的样子

<div class="modal fade" id="addNewPaymentMethod" role="dialog">
  <div class="modal-dialog">
   .
   .
   .
   <button type="button" id="close-modal" data-dismiss="modal" style="display: none">Close</button>
  </div>
</div>

现在,当您想以编程方式关闭模式时,只需在该按钮上触发一个点击事件,该按钮对用户不可见

在 Javascript 中,您可以触发单击该按钮,如下所示:

document.getElementById('close-modal').click();

i
ibrahim saputra

经过一番测试,我发现对于 bootstrap modal 执行 $(.modal).modal('show') 后需要等待一段时间才能执行 $(.modal).modal('hide')。我发现在我的情况下,两者之间至少需要 500 毫秒的间隔。
所以这是我的测试用例和解决方案:

$('.modal-loading').modal('show');
setTimeout(function() {
  $('.modal-loading').modal('hide');
}, 500);

这解决了我打开另一个模态时没有关闭的问题,谢谢
P
Phatyh T.

您可以使用;

$('#' + $('.modal.show').attr('id')).modal('hide');

V
Vikalp Veer

使用 modal.hide 只会隐藏模式。如果您在模态下方使用叠加层,它仍然会存在。使用点击调用实际关闭模式并删除覆盖。

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

M
Muhammad Bilal

另一种方法是首先删除类modal-open,它会关闭模态。然后你移除移除模态的灰色覆盖的类modal-backdrop。

可以使用以下代码:

$('body').removeClass('modal-open')
$('.modal-backdrop').remove()  

请尽量避免仅仅将代码作为答案,并尝试解释它的作用和原因。对于没有相关编码经验的人来说,您的代码可能并不明显。请编辑您的答案以包括 clarification, context and try to mention any limitations, assumptions or simplifications in your answer.
好的,所以基本上它的作用是删除类modal-open,它关闭了模态。然后删除类 modal-backdrop 删除模态的灰色覆盖
V
Vasilisa

就我而言,我使用了一个按钮来显示模态

<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 打字稿中):

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>

d
danklad

这对我有用。我是新手,所以请忽略我对 JQuery 和 Vanilla JavaScript 的使用。

document.addEventListener("click",function(){
        $(".modal").modal('hide');
        });

u
user3870075

这段代码非常适合我关闭模式(我正在使用引导程序 3.3)

$('#myModal').removeClass('in')

a
alexP

在我的情况下,触发引导模式的主页在使用 $("#modal").modal('toggle'); 方式后开始没有响应,但开始以以下方式响应:

$("#submitBtn").on("click",function(){
  $("#submitBtn").attr("data-dismiss","modal");
});

E
Edgar256

这很好用

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

但是,当您有多个模态堆叠在一起时,它是无效的,所以相反,这是可行的

data-dismiss="modal"

M
Michael Nelles

只需在模态中添加它

div tabindex="-1"

s
slashka

此外,您可以“单击”关闭对话框的“x”。例如:

$(".ui-dialog-titlebar-close").click();


V
Venkatachalam

有时解决方案不起作用,因此您必须正确删除 in 类并手动添加 css display:none 。

$("#modal").removeClass("in");
$("#modal").css("display","none");

Z
Zoltán Süle

我的回答与上述许多其他问题并不严格相关。但是这些对话帮助我弄清楚为什么当我点击一个非常通用的关闭按钮时我的引导模式没有反应,而当我按下 ESC 按钮时它仍然关闭它。

<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>

它可能会帮助某人。

如果您输入代码(在某些特殊情况下是有意义的):

$modal.off('click')

那么它也会导致模式不会因为事件被阻止而关闭。

在这种情况下,您必须自己处理:

$modal
  .on('click', '[data-dismiss="modal"]', function() {
      $modal.modal('hide');
  });

谢谢,这对我有用。 data-dismiss='modal' aria-hidden='true'
E
Ehsäɳ Khʌɳ

尝试这个

 $('#yourmodal').click(); 

佚名

你可以试试这个只是添加

div tabindex="-1"

在您的 HTML 和 jquery 中,您可以使用

$('#myModal').modal('hide')

或者

$('#myModal').modal('toggle')