模式未在href上打开

时间:2019-05-02 12:49:33

标签: javascript jquery html bootstrap-modal href

当我单击 a href = .... (特权1)时,它应该打开模式,但没有打开。尽管网址栏更改为 project / index.php#privilege_10

$(document).ready(function() {

  $(window.location.hash).modal('show');

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<a href="#privilege_10" class="btn btn-lg btn-dark">Privilege</a>

<!-- The Modal -->
<div class="modal" id="privilege_10">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Privilege Settings for
          <font color="blue">Name</font>
        </h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        <form method="post" action="/jainvidhya/subdomains/teacher/teacher.php?id=10">
          <span class="float-right">Quiz Settings&nbsp;&nbsp;
		  <input type="checkbox" name="Privilege_Quiz_Name" id="Privilege_Quiz_Id" value="Y"checked='checked'/></span>
          <br />
          <span class="float-right">Question Settings&nbsp;&nbsp;
		  <input type="checkbox" name="Privilege_Question_Name" id="Privilege_Question_Id" value="Privilege_Question_Value"checked='checked'/></span>
          <br />
          <span class="float-right">Student Settings&nbsp;&nbsp;
		  <input type="checkbox" name="Privilege_Student_Name" id="Privilege_Student_Id" value="Y"checked='checked'/></span>
      </div>

      <!-- Modal footer -->
      <div class="modal-footer">
        <input type="submit" class="btn btn-lg btn-info" name="submit" value="Submit">
        </form>
      </div>

    </div>
  </div>
</div>


<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

当我单击 a href = .... (特权1)时,它应该打开模式,但没有打开。尽管网址栏更改为 project / index.php#privilege_10

2 个答案:

答案 0 :(得分:1)

也许您在选择器中缺少#:

$(`#${window.location.hash}`).modal('show');

答案 1 :(得分:0)

借助Walks's comment 的帮助,为data-toggle="modal" data-target="#privilege_10"标签添加了a属性解决了这个问题。 / p>

$(document).ready(function() {

  $(window.location.hash).modal('show');

});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<a href="#privilege_10" class="btn btn-lg btn-dark" data-toggle="modal" data-target="#privilege_10">Privilege</a>

<!-- The Modal -->
<div class="modal" id="privilege_10">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Privilege Settings for
          <font color="blue">Name</font>
        </h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        <form method="post" action="/jainvidhya/subdomains/teacher/teacher.php?id=10">
          <span class="float-right">Quiz Settings&nbsp;&nbsp;
		  <input type="checkbox" name="Privilege_Quiz_Name" id="Privilege_Quiz_Id" value="Y"checked='checked'/></span>
          <br />
          <span class="float-right">Question Settings&nbsp;&nbsp;
		  <input type="checkbox" name="Privilege_Question_Name" id="Privilege_Question_Id" value="Privilege_Question_Value"checked='checked'/></span>
          <br />
          <span class="float-right">Student Settings&nbsp;&nbsp;
		  <input type="checkbox" name="Privilege_Student_Name" id="Privilege_Student_Id" value="Y"checked='checked'/></span>
      </div>

      <!-- Modal footer -->
      <div class="modal-footer">
        <input type="submit" class="btn btn-lg btn-info" name="submit" value="Submit">
        </form>
      </div>

    </div>
  </div>
</div>


<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>