【问题标题】:Why I'm not able to show second modal dialog after closing the first modal dialog?为什么我在关闭第一个模态对话框后无法显示第二个模态对话框?
【发布时间】:2020-03-31 02:00:59
【问题描述】:

我正在为我的网站使用 Bootstrap v3.3.5

我有以下第一个引导模式对话框的 HTML 代码:

<div style="display: block;" id="myModal-event" class="modal fade in" role="dialog">
  <div role="document" style="width:600px;position:relative;margin:auto;margin-top:10px;">
    <div class="modal-content" style="border:0;"> 
      <div style="margin-top: 10px;" role="document" class="modal-dialog event-planner">
        <div class="modal-content">
          <div class="modal-header">
            <h4 id="myModalLabel" class="modal-title">Event Details</h4>
          </div>
          <div class="modal-body">
            <div class="row">
              <div class="col-sm-6 col-md-6 col-sm-12">
                <div class="event-title">
                NewDemoEvent5
                <input name="hid_event_id" id="hid_event_id" value="501" type="hidden">
                </div>
                <ul id="popover-content" class="list-group" style="display: none">
                  <a href="#" data-target="#myModal-edit-event" class="list-group-item" id="edit-event">Edit Event</a>
                  <a href="#" class="list-group-item">Invite Members</a>
                  <a href="#" class="list-group-item">Delete Event</a>
                </ul>
              </div>
            </div>
            <hr>
          </div>
          <div class="modal-footer">
            <button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

当用户点击菜单超链接“编辑事件”时,上面的模态对话框应该关闭,一个新的模态对话框应该打开。

以下是新模态对话框的 HTML:

<div id="myModal-edit-event" class="modal fade" role="dialog">
  <div role="document" style="width:600px;position:relative;margin:auto;margin-top:10px;">
    <div class="modal-content" style="border:0;">
      <div class="modal-header">
        <h4 id="myModalLabel" class="modal-title">Event Details</h4>
      </div>
      <div class="modal-body"> Loading Mannu... </div>
      <div class="modal-footer">
        <button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
      </div>
    </div>    
  </div>
</div>

我可以在单击“编辑事件”超链接时关闭第一个模式对话框,但我无法打开新的第二个模式对话框。

我试过下面的代码:

$('#myModal-event').on('click', '#edit-event', function (event) {
  event.preventDefault();
  $(this).closest('.modal').modal('hide');
  $('#myModal-edit-event').modal('show');
});

此外,用于某些其他功能的其他脚本也会被执行。我强烈认为它也影响了我的代码。该脚本如下。

$(document).ready(function() {
  $('body').on('hidden.bs.modal', '#myModal-event', function () {
    console.log('Hi *');
    $("#myModal-event .modal-body").html(' Loading... ');
    $(this).removeData('bs.modal');    
    });
});

在上面的脚本中,我想知道在语句console.log('Hi *'); 之后执行是否停止并且最后两个语句没有被执行。

请有人帮我解决我的问题。

谢谢。

【问题讨论】:

  • 只是把它扔在那里......当你真的让它工作......你的用户体验设计将成为用户点击浏览器后退和前进按钮期望在模式之间切换的噩梦。我遇到了同样的事情。如果您将细节放入模态并希望搜索引擎索引这些只能通过模态获得的细节,那么 SEO 也将是一场噩梦

标签: javascript jquery twitter-bootstrap twitter-bootstrap-3 bootstrap-modal


【解决方案1】:

在 JS 脚本中,以下行可能会导致第二个模态不显示,因为它有 .modal 类,这在两个模态中都很常见

 $(this).closest('.modal').modal('hide');

第二个原因可能是&lt;a&gt; 标签中没有data-toggle 属性,它打开了第二个模式。

<a href="#" data-target="#myModal-edit-event" class="list-group-item" id="edit-event">Edit Event</a>

data-toggledata-target 两个数据属性都是必需的,没有其他属性将无法工作。

对于关闭第一个模式并打开第二个模式的简单方法

&lt;a&gt; 标记中删除href="#" data-target="#myModal-edit-event" id="edit-event"

无需点击功能

$('#myModal-event').on('click', '#edit-event', function (event) {
});

带有数据属性data-dismiss="modal"的简单关闭第一个模态

<a data-dismiss="modal" class="list-group-item">Edit Event</a>

一旦第一个模态关闭,使用模态事件hidden.bs.modal 打开第二个模态。

$(document).ready(function(){
    $('#myModal-event').on('hidden.bs.modal', function () {
        $('#myModal-edit-event').modal('show');
     });
});

工作示例

$(document).ready(function(){
	$('#myModal-event').on('hidden.bs.modal', function () {
		$('#myModal-edit-event').modal('show');
	 });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal-event">Open Modal</button>
<div id="myModal-event" class="modal fade in" role="dialog">
  <div role="document" style="width:600px;position:relative;margin:auto;margin-top:10px;">
    <div class="modal-content" style="border:0;"> 
      <div style="margin-top: 10px;" role="document" class="modal-dialog event-planner">
        <div class="modal-content">
          <div class="modal-header">
            <h4 id="myModalLabel" class="modal-title">Event Details</h4>
          </div>
          <div class="modal-body">
            <div class="row">
              <div class="col-sm-6 col-md-6 col-sm-12">
                <div class="event-title">
                NewDemoEvent5
                <input name="hid_event_id" id="hid_event_id" value="501" type="hidden">
                </div>
                <ul id="popover-content" class="list-group" style="display: none">
                  <a data-dismiss="modal">Edit Event</a>
                  <a href="#" class="list-group-item">Invite Members</a>
                  <a href="#" class="list-group-item">Delete Event</a>
                </ul>
              </div>
            </div>
            <hr>
          </div>
          <div class="modal-footer">
            <button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div id="myModal-edit-event" class="modal fade" role="dialog">
  <div role="document" style="width:600px;position:relative;margin:auto;margin-top:10px;">
    <div class="modal-content" style="border:0;">
      <div class="modal-header">
        <h4 id="myModalLabel" class="modal-title">Event Details</h4>
      </div>
      <div class="modal-body"> Loading Mannu... </div>
      <div class="modal-footer">
        <button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
      </div>
    </div>    
  </div>
</div>

旁注:您在 First Modal 中有这个重复的 modal-content

<div class="modal-content" style="border:0;"> 
  <div style="margin-top: 10px;" role="document" class="modal-dialog event-planner">
    <div class="modal-content">
    //Rest of the Modal Content
    </div>
  </div>
</div>

【讨论】:

  • 非常感谢您的大力帮助和周到的指导。但问题仍然存在。我完全尝试了您在答案中使用的代码。当我单击“编辑事件”链接时,第一个模态对话框关闭,但随后第二个模态对话框没有打开,并且出现了一个黑色的褪色屏幕。我也检查了控制台。它显示了字符串“Hi *”。这表明我在问题末尾放置的代码正在执行。该代码是否阻止打开下一个模式对话框?如果您能指导我并解决问题,我将不胜感激。再次感谢。等待您的回复。
  • 即使我尝试输入代码 $('body').on('hidden.bs.modal', '#myModal-event', function () { console.log('Hi * '); $("#myModal-event .modal-body").html('加载中...'); $(this).removeData('bs.modal'); });在 cmets 内部,但在关闭第一个模态对话框并且第二个模态对话框没有打开后仍然出现黑色淡出屏幕。我现在变得越来越没有头绪了。也没有错误,firebug控制台中没有输出数据。
  • 在 2nd modal 的 'modal-content' 之前添加类 'modal-dialog' 并查看它是否有影响,并检查两个 modals 的所有 div 是否正确关闭
  • 我尝试在 2nd modal 的 'modal-content' 之前添加类 'modal-dialog' 。但结果还是一样。
  • 2 个选项,1 如果你有实时链接,分享所以仔细看看,2. 如果 localhost,创建一个pastebin.com 的代码(首选 HTML 代码)并分享这里的链接将检查代码
【解决方案2】:

尝试以下解决方案。

$('#edit-event').on('click', function(){
    /* Hide event modal */
    $('#myModal-event').modal('hide');

    /* Show edit event modal */
    $('#myModal-edit-event').modal('show');
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    相关资源
    最近更新 更多