我正在尝试使用Bootstrap 4 Modal代码修复我的代码中的奇怪错误。我只是从这里复制并粘贴了代码:https://getbootstrap.com/docs/4.0/components/modal/#events
这是针对汽车网站的,Im尝试根据要求通过data属性传递库存和VIN。问题是这如何与foreach循环一起使用。它可以在单个Wordpress页面或Post上运行,但不会从循环的每个部分提取数据。
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-stock="<?php echo $vehicle['stock_number'];?>">Check Availability</button>
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Stock</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Recipient:</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Message:</label>
<textarea class="form-control" id="message-text"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Send message</button>
</div>
</div>
</div>
</div>
还有我的Jquery代码 (函数($){
$('#exampleModal').on('modal.fade.show', function (event) {
alert( "clicked" );
var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('stock') // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use $ here, but you could use a data binding library or other methods instead.
var modal = $(this)
modal.find('.modal-title').text('New message to ' + recipient)
modal.find('.modal-body input').val(recipient)
})})( jQuery );```
从功能上来说,这是可行的,只是不在foreach循环中生成的动态页面内。这些按钮已应用了适当的数据属性,但数据似乎没有从循环传递到模式。我有一个开发页面,您可以在此处进行实时查看,有问题的按钮是“检查可用性” http://windsor.sixomedia.ca/inventory/New/
答案 0 :(得分:0)
尝试一下
var recipient = button.data('data-stock');
希望有帮助。