我有一个在单击“编辑”按钮时打开的模式,现在我想要 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>
最佳答案
您可以使用event.latedTarget
函数有更多动态
方法来获取数据属性
。
您不需要需要在此处使用内联onclick
函数,这可以使用 native jQuery
Bootstrap 函数(如)轻松完成show.bs.modal
并获取点击时的 a
数据属性并将其显示在模态中。
$("#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">×</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>
关于php - Laravel中点击模态后如何获取标签的data-href值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63894870/