Laravel中点击模态后如何获取标签的data-href值

我有一个在单击“编辑”按钮时打开的模式,现在我想要 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>


Helenr
浏览 123回答 3
3回答

沧海一幻觉

您可以使用event.relatedTarget函数有更多dynamic方法来获取data-attributes.您不需要在此处使用内联函数,这可以使用本机引导函数onclick轻松完成,例如单击并获取数据属性并在模式中显示。jQueryshow.bs.modala$("#editModal").on('show.bs.modal', function(event) {&nbsp; var button = $(event.relatedTarget) //Button that triggered the modal&nbsp; var getHref = button.data('href') //get button href&nbsp; $('#href').text(getHref) //show in the modal})现场工作演示:$("#editModal").on('show.bs.modal', function(event) {&nbsp; var button = $(event.relatedTarget) //Button that triggered the modal&nbsp; var getHref = button.data('href') //get button href&nbsp; console.log(getHref) //{{ url('admin/product_edit/'.$cat->id) }}&nbsp; $('#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">&nbsp; <div class="modal-dialog" role="document">&nbsp; &nbsp; <div class="modal-content">&nbsp; &nbsp; &nbsp; <div class="modal-header">&nbsp; &nbsp; &nbsp; &nbsp; <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="close" data-dismiss="modal" aria-label="Close">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span aria-hidden="true">&times;</span>&nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="modal-body">&nbsp; &nbsp; &nbsp; &nbsp; <span id="href"></span>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="modal-footer">&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-primary">Save changes</button>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; </div></div>

精慕HU

你可以这样做<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) {&nbsp; &nbsp; console.log(inp.getAttribute('data-href'));}

慕桂英3389331

如果你使用 jQuery,那么你可以这样做let hrefVal = null$('.edit_product').click(function () { hrefVal = $(this).data('href') })
打开App,查看更多内容
随时随地看视频慕课网APP