亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel中點擊模態后如何獲取標簽的data-href值

Laravel中點擊模態后如何獲取標簽的data-href值

PHP
Helenr 2024-01-19 20:48:25
我有一個在單擊“編輯”按鈕時打開的模式,現在我想要 data-href 的值,在單擊以回顯模式主體內部時打開模式,我該怎么做<a href="javascript:void(0);" data-href="{{ url('admin/product_edit/'.$cat->id) }}" class="edit_product btn btn-sm btn-primary" data-toggle="modal" data-target="#editModal"> Edit</a>
查看完整描述

3 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

您可以使用event.relatedTarget函數有更多dynamic方法來獲取data-attributes.


您不需要在此處使用內聯函數,這可以使用本機引導函數onclick輕松完成,例如單擊并獲取數據屬性并在模式中顯示。jQueryshow.bs.modala


$("#editModal").on('show.bs.modal', function(event) {

  var button = $(event.relatedTarget) //Button that triggered the modal

  var getHref = button.data('href') //get button href

  $('#href').text(getHref) //show in the modal

})

現場工作演示:


$("#editModal").on('show.bs.modal', function(event) {

  var button = $(event.relatedTarget) //Button that triggered the modal

  var getHref = button.data('href') //get button href

  console.log(getHref) //{{ url('admin/product_edit/'.$cat->id) }}

  $('#href').text(getHref) //show in the modal

})

<!-- Latest compiled and minified CSS -->

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

<!-- jQuery library -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- Popper JS -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

<!-- Latest compiled JavaScript -->

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>


<a href="javascript:void(0);" data-href="{{ url('admin/product_edit/'.$cat->id) }}" class="edit_product btn btn-sm btn-primary" data-toggle="modal" data-target="#editModal"> Edit</a>


<!-- Modal -->

<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">

  <div class="modal-dialog" role="document">

    <div class="modal-content">

      <div class="modal-header">

        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>

        <button type="button" class="close" data-dismiss="modal" aria-label="Close">

          <span aria-hidden="true">&times;</span>

        </button>

      </div>

      <div class="modal-body">

        <span id="href"></span>

      </div>

      <div class="modal-footer">

        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>

        <button type="button" class="btn btn-primary">Save changes</button>

      </div>

    </div>

  </div>

</div>


查看完整回答
反對 回復 2024-01-19
?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

你可以這樣做


<a href="javascript:void(0);" data-href="/sample/url" class="edit_product btn btn-sm btn-primary" data-toggle="modal" data-target="#editModal" onclick='process(this)'> Edit</a>


function process(inp) {

    console.log(inp.getAttribute('data-href'));

}


查看完整回答
反對 回復 2024-01-19
?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

如果你使用 jQuery,那么你可以這樣做


let hrefVal = null


$('.edit_product').click(function () { hrefVal = $(this).data('href') })


查看完整回答
反對 回復 2024-01-19
  • 3 回答
  • 0 關注
  • 256 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號