简体   繁体   English

如何将值传递给Bootstrap模式的href链接?

[英]How to pass value into href link of a Bootstrap Modal?

I want to clear the values of a certain table so I want to get the table name using Bootstrap modal. 我想清除某个表的值,所以我想使用Bootstrap模式获取表名。 I want the name of the table in url. 我想要url中表的名称。

This is my code: Here I'm sending the name of the table into the modal 这是我的代码:这里我将表的名称发送到模态中

<ol class="breadcrumb text-center">
  <li class="breadcrumb-item">
     <a class="btn btn-primary" href="#scrapModal" data-toggle="modal" data-table="product1">Insert1</a>
     <a class="btn btn-primary" href="#scrapModal" data-toggle="modal" data-table="product2">Insert2</a>
     <a class="btn btn-primary" href="#scrapModal" data-toggle="modal" data-table="product3">Insert3</a>
     <a class="btn btn-primary" href="#scrapModal" data-toggle="modal" data-table="product4">Insert4</a>
</ol>

I want to pass the value inside data-table into href as clear.php?clear_id="table_name" 我想将data-table中的值传递给href,如clear.php?clear_id =“table_name”

<div class="modal fade" id="scrapModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel1" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel1">Confirm Clear?</h5>
        <button class="close" type="button" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">Are you sure you want to clear scrap for this table.</div>
      <div class="modal-footer">
        <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
        <a class="btn btn-danger" href="clear.php?clear_id=">Clear</a>
      </div>
    </div>
  </div>
</div>

Thank you in advance! 先感谢您!

You can dynamically change the href value of the Clear button in your modal by utilising the shown.bs.modal event trigger and building the URL with the data-table attribute at your disposal provided by the relatedTarget property in the event object. 您可以动态地改变href利用你的模式清除键的值shown.bs.modal事件触发和建立与该URL data-table在您的处置由提供的属性relatedTarget事件对象的属性。

I've add a clear button click event to emphasise this visually. 我添加了一个清晰的按钮点击事件,以便在视觉上强调这一点。

 $('#scrapModal').on('show.bs.modal', function (e) { var table = $(e.relatedTarget).data('table') var href = 'clear.php?clear_id=' + table $('.btn-danger', this).attr('href', href) console.log(href) }) // Simulate "clear" button click to alert href value $('#scrapModal .btn-danger').on('click', function (e) { e.preventDefault() alert(e.target.pathname + e.target.search) }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/> <ol class="breadcrumb text-center"> <li class="breadcrumb-item"> <a class="btn btn-primary" href="#scrapModal" data-toggle="modal" data-table="product1">Insert1</a> <a class="btn btn-primary" href="#scrapModal" data-toggle="modal" data-table="product2">Insert2</a> <a class="btn btn-primary" href="#scrapModal" data-toggle="modal" data-table="product3">Insert3</a> <a class="btn btn-primary" href="#scrapModal" data-toggle="modal" data-table="product4">Insert4</a> </li> </ol> <div class="modal fade" id="scrapModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel1" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel1">Confirm Clear?</h5> <button class="close" type="button" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&#215;</span> </button> </div> <div class="modal-body"> Are you sure you want to clear scrap for this table. </div> <div class="modal-footer"> <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button> <a class="btn btn-danger" href="#">Clear</a> </div> </div> </div> </div> 

Use the following lines of code inside your script tag and this should work fine. 在脚本标记内使用以下代码行,这应该可以正常工作。

$('#scrapModal .btn-danger').on('click', function (e) {
  e.preventDefault()
  alert(e.target.pathname + e.target.search)
})

You can do it by using a simple script like this: 你可以使用这样一个简单的脚本来做到这一点:

$('#scrapModal').on('show.bs.modal', function (event) {
  var table = $(event.relatedTarget).data('table');
  $(this).find('.btn-danger').attr("href", "clear.php?clear_id=" + table);
)}

This will run when the modal appears. 这将在模态出现时运行。 It would be better to give the confirm button an id attribute in order to look up in a more elegant way. 最好给确认按钮一个id属性,以便以更优雅的方式查找。

You can find more information about varying the modal content here: https://getbootstrap.com/docs/4.0/components/modal/#varying-modal-content 您可以在此处找到有关改变模态内容的更多信息: https//getbootstrap.com/docs/4.0/components/modal/#varying-modal-content

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

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