简体   繁体   English

无法通过jQuery关闭Bootstrap模态

[英]Can't close Bootstrap modal via jquery

I am having problems closing a Bootstrap modal window via jquery (eg $('#modalName').modal('hide');). 我在通过jquery关闭Bootstrap模态窗口时遇到问题(例如$('#modalName')。modal('hide');)。

The following is the basic structure of all the relevant parts contained within the main page (index.cfm): 以下是主页(index.cfm)中包含的所有相关部分的基本结构:

The modal's markup: 模态的标记:

<div class="modal fade modal-wide" id="addEventModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <!--- Content inserted here --->
    </div>
  </div>
</div>

Link to open modal: 链接到开放模式:

<a data-toggle="modal" href="AddEventModal_Form.cfm?listOfParams" data-target="#addEventModal" class="btn btn-primary btn-sm btn-addEvent"><span class="glyphicon glyphicon-plus"></span> Add</a>

The following jquery is then called with the above link is clicked. 然后,单击上面的链接,调用以下jquery。 This refreshes the contents of the modal to make sure I'm displaying the correct information each time. 这刷新了模态的内容,以确保我每次都显示正确的信息。

//Refresh the modal's contents prior to display 
  $(document).on("click", ".btn-viewAll, .btn-addEvent", function(e){   
    $($(this).attr('data-target')).removeData('bs.modal');
  });

This is then triggered one the modal has been displayed on the screen. 然后在屏幕上已显示模态的情况下触发此操作。 It just adjusts the height of the modal's textarea via a jquery library and then sets the cursor at the end of any imported text. 它只是通过jquery库调整模态文本区域的高度,然后将光标设置在任何导入文本的末尾。

$('#addEventModal').on('shown.bs.modal', function(){
    //Adjust the textarea's number of rows
    $('textarea').autosize();

    //Set focus to end of text in textarea. Doesn't work in older versions of IE.
    var el = $("#currentEventText").get(0);
    var elemLen = el.value.length;

    el.selectionStart = elemLen;
    el.selectionEnd = elemLen;
    el.focus();

    //Process the form via Ajax and close the modal window
    $("#submitButton").on('click', function(){
      //Process the form data via Ajax here...

      //Close the modal window  
      $('#addEventModal').modal('hide');
    });
  });

As you can see, what I also want it to do is listen for when the submit button is clicked and then process the form data via Ajax and close the modal. 如您所见,我还希望它执行的操作是单击“提交”按钮时监听,然后通过Ajax处理表单数据并关闭模式。 I can get it to do the Ajax part but the modal never closes. 我可以将其用作Ajax部分,但模态永远不会关闭。

Here's the basic structure of the AddEventModal_Form.cfm file that is imported into the addEventModal's modal-content each time: 这是AddEventModal_Form.cfm文件的基本结构,该文件每次都会导入到addEventModal的模态内容中:

<div class="modal-header">
  <h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">  
  <textarea name="currentEventText" id="currentEventText" class="form-control textarea-animate" rows="6"></textarea>     

  <div class="button-div">
    <a href="javascript:void(0);" class="btn btn-default btn-sm" onclick="javascript:$('#currentEventText').val('').trigger('autosize.resize');"> Clear</a>  
    <a href="javascript:void(0);" id="submitButton" class="btn btn-primary btn-sm">Submit</a>
  </div>
</div>

What am I doing wrong? 我究竟做错了什么?

If you hide the modal dialog it will not get closed. 如果隐藏模式对话框,它将不会关闭。

Try this: 尝试这个:

$("#submitButton").on('click', function() {
    $('#addEventModal').close();
});

添加data-dismiss =“ modal”应该可以:

 <a href="javascript:void(0);" id="submitButton" class="btn btn-primary btn-sm" data-dismiss="modal">Submit</a>

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

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