简体   繁体   English

如何通过链接将变量传递给模式

[英]How to pass variables to a modal from a link

I have a navigation bar with some links. 我有一个带有一些链接的导航栏。 The links all trigger the same modal but I need to display different information inside the modal based on the link. 链接都触发相同的模态,但是我需要根据链接在模态内部显示不同的信息。 I'd prefer a non-jQuery solution but if it is necessary I'll take it. 我希望使用非jQuery解决方案,但如果有必要,我会考虑使用它。

I tried this... 我试过了

firstpage.php firstpage.php

    <li><a href="#theModal?viewmessages=mymessages" data-toggle="modal">My Messages</a></li>
    <li><a href="#theModal?viewnotifications=mynotifications" data-toggle="modal">My Notifications</a></li>

<!---Modal -->
  <div id="theModal" class="modal fade text-center">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">
          <?php include 'modal.php';?>
      </div>
    </div>
  </div>
 <!-- Modal -->

modal.php modal.php

<!--
=============================
ISSET_GET MESSAGES 
=============================-->
<div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Messages</h4>
      </div>
      <div class="modal-body">
        <p>some text here</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
<!--
=============================
ISSET_GET NOTIFICATIONS
=============================-->
<div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Notifications</h4>
      </div>
      <div class="modal-body">
        <p>some text here</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>

I think you should render all of your modals at the bottom of the page and use jQuery to to load the modal associated to the link. 我认为您应该在页面底部呈现所有模态,并使用jQuery加载与链接关联的模态。

eg 例如

<button type="button" class="btn btn-info btn-lg" id="myBtn1">Open Modal 1</button>

Then bind bootstraps .modal() function to the button's click event with jQuery. 然后使用jQuery将bootstraps .modal()函数绑定到按钮的click事件。 This is where you can set which modal to load eg. 您可以在此处设置要加载的模式,例如

$("#myBtn1").click(function(){
    $("#myModal1").modal();
});

Please run my code snippet which I have modified from w3 schools documentation 请运行我从W3学校文档中修改的代码段

 <!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.2.0/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>Activate multiple Modal with JavaScript</h2> <!-- Trigger the modal with a button --> <button type="button" class="btn btn-info btn-lg" id="myBtn1">Open Modal 1</button> <button type="button" class="btn btn-success btn-lg" id="myBtn2">Open Modal 2</button> <!-- Modal --> <div class="modal fade" id="myModal1" 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">Look its modal one</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 class="modal fade" id="myModal2" 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">look its modal 2</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> <script> $(document).ready(function(){ $("#myBtn1").click(function(){ $("#myModal1").modal(); }); $("#myBtn2").click(function(){ $("#myModal2").modal(); }); }); </script> </body> </html> 

original source of code that I modified: 我修改过的原始代码来源:

https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_modal_js&stacked=h https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_modal_js&stacked=h

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

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