简体   繁体   English

Bootstrap模式不会在单击时关闭

[英]Bootstrap modal won't close on click

I am very new to web dev. 我是Web开发人员的新手。 Still not too familiar with JavaScript. 仍然不太熟悉JavaScript。 I am making an error message using that will load using code one of our dev guys created for a different page. 我正在使用一条错误消息,该错误消息将使用我们为不同页面创建的开发人员之一的代码加载。 I created modals that show on a click event and then close on a click event. 我创建了在点击事件中显示然后在点击事件中关闭的模式。 For this one I am having the modal show based on returned URL parameters. 为此,我将基于返回的URL参数进行模式展示。 And I am using code I copied from another page one of our dev guys made (not with Bootstrap modals). 我使用的是我从另一个开发人员复制的页面复制的代码(不使用Bootstrap模态)。

The code below makes the modal show, but the buttons used to close the modal don't work. 下面的代码显示了模态,但是用于关闭模态的按钮不起作用。 Not sure why. 不知道为什么。

This is the tag that will append the URL when returned: 这是返回时将附加URL的标记:

(**...?companyName=ACME47 & err1=0 & err2=0 & err3=1**)

When the error values are 1 is loads the page to show the error modal with the text for that error in it. 当错误值为1时,将加载页面以显示错误模态,并在其中显示该错误的文本。

Here is the code I'm using for the form (not attaching the style sheet, so it looks different). 这是我用于表单的代码(未附加样式表,因此看起来有所不同)。

jQuery(document).ready(function() {

  //  jQuery code snippet to get the dynamic variables stored in the url as parameters and store them as JavaScript variables ready for use with your scripts

  $.urlParam = function(name) {
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results == null) {
      return null;
    } else {
      return results[1] || 0;
    }
  }

  // Get url parameters

  var err1 = new Number($.urlParam('err1'));
  if (isNaN(err1)) {
    err1 = new Number(0);
  }
  var err2 = new Number($.urlParam('err2'));
  if (isNaN(err2)) {
    err2 = new Number(0);
  }
  var err3 = new Number($.urlParam('err3'));
  if (isNaN(err3)) {
    err3 = new Number(0);
  }

  console.log('err1: ' + err1); // Writes a message to the browser console [f12]
  console.log('err2: ' + err2); // Writes a message to the browser console [f12]
  console.log('err3: ' + err3); // Writes a message to the browser console [f12]
  console.log('CompanyName: ' + $.urlParam('companyName')); // Writes a message to the browser console [f12]

  // Display error message function

  // Read a page's GET URL variables and return them as an associative array.

  if (err1 > 0) {
    $("#error-box").show();
    $("#error-1").show();
  };
  if (err2 > 0) {
    $("#error-box").show();
    $("#error-2").show();
  };
  if (err3 > 0) {
    $("#error-box").show();
    $("#error-3").show();
  };

});
<div class="modal" id="error-box" style="display: none;" tabindex="-1" role="dialog" aria-labelledby="error-boxLabel">
  <div class="modal-dialog" role="document" style="height:200px;">
    <div class="modal-content" style="height: 100%;">
      <div class="modal-header alert-danger">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title text-danger">Form Submission Error</h4>
      </div>
      <div class="modal-body alert-danger" style="height: 100%;">
        <div class="textWrapper">
          <ul>
            <li id="error-1" style="display: none;">That email address is already in use. (01)</li>
            <li id="error-2" style="display: none;">A company with that name already has an account in this system. (02)</li>
            <li id="error-3" style="display: none;">An unknown error has occurred. If your E-Mail or Phone Number was correctly filled in, you will be contacted shortly. (03)</li>
          </ul>
        </div>
      </div>
      <div class="modal-footer modal-footer-text">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

You shouldn't be using jQuery's show() method to show the modal. 您不应该使用jQuery的show()方法显示模式。 Doing so doesn't register it with Bootstrap, so there's nothing to close with elements possessing the data-dismiss attribute. 这样做不会在Bootstrap中注册它,因此对于拥有data-dismiss属性的元素没有什么可关闭的。 Use Bootstrap's method : 使用Bootstrap的方法

$("#error-box").modal('show');

Note that you may also need to use Bootstrap's show.bs.modal callback to show your error containers. 请注意,您可能还需要使用Bootstrap的show.bs.modal回调来显示错误容器。 It's possible that they won't be available to jQuery until the modal is initiated. 在启动模式之前,jQuery可能无法使用它们。

This is what I think you want see fiddle https://jsfiddle.net/sxh0n7d1/53/ 这就是我想看到的小提琴https://jsfiddle.net/sxh0n7d1/53/

Note: I set all the errors to visible in the fiddle, so that there is something to see how it works. 注意:我将所有错误设置为在小提琴中可见,以便可以看到一些错误。

add this to your javascript 将此添加到您的JavaScript

  $('.close, .btn-danger').click(function() {
        $( "#error-box" ).hide();
  });

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

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