Bootstrap模式不会与location.href一起显示

时间:2018-02-07 07:30:57

标签: javascript html django twitter-bootstrap

我希望首先弹出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位置错误

2 个答案:

答案 0 :(得分:0)

问题是您在模态打开后重定向到新位置,并且没有时间让您看到它打开。当您重定向到新位置时,所有dom状态都会刷新。

答案 1 :(得分:0)

**找到解决方案。只需使用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>