Bootstrap Modal不会显示location.href



我希望Bootstrap模态首先弹出,我将从中获取一些数据,并且使用该数据,我想去HREF位置。

function callModal(curr){
    var currele = curr.id;
    alert(currele);
    switch (currele){
        case "add_resource":
        {
            $("#myModal").modal('show');
            window.location.href="add/resource";     
            break;
        }
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>Modal Example</h2>
  
  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
  
</div>
<div>
<a href="#" onClick= "callModal(this)" id = "add_resource" >add resouce</a><br>
</div>
</body>
</html>

问题是:仅称为HREF,而不是模态如果我删除HREF调用,那么模态就会被称为罚款,但没有被调用。

**在代码段中,您可以忽略未定义的HREF位置错误

问题在于,您在模态打开后将其重定向到新位置,并且没有时间可以看到它打开。重定向到新位置时,所有的DOM状态都将刷新。

**找到了解决方案。仅使用2个单独的功能就可以解决它。:) **

<!-- begin snippet: js hide: false console: true babel: false -->
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
  <div class="container">
    <h2>Modal Example</h2>
    <!-- Modal -->
    <div class="modal fade" id="myModal" role="dialog">
      <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Modal Header</h4>
          </div>
          <div class="modal-body">
            <p>Some text in the modal.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div>
    <a href="#" onClick="callModal(this)" id="add_resource">add resouce</a><br>
  </div>
</body>
</html>

var direction = "Saurabh Adhikary is best";
function callModal(curr){
    
    var currele = curr.id;
    alert(currele);
    switch (currele){
        case "add_resource":
        {
            $("#myModal").modal('show');
            direction ="add/resource";     
            break;
        }
}
}
function printer()
{
alert(direction);
location.href = direction;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>Modal Example</h2>
  
  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p >Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" onClick= printer() class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
  
</div>
<div>
<a href="#" onClick= "callModal(this)" id = "add_resource" >add resouce</a><br>
</div>
</body>
</html>

最新更新