javascript - 在 php foreach 函数中单击按钮时按 id 显示模式
问题描述
我在phpforeach
的函数中有一个按钮。我有一个模式,其id对应于单击的按钮。现在,我想要做的是,当我单击按钮时,它会按 id 显示模态,但第一个按钮只能显示模态,而另一个按钮则不能。我应该怎么办?这是我的代码...
<?php $count = 0; ?>
<?php foreach ($land_i as $li){ ?>
<?php $count++ ?>
<tr>
<td><?= $li['date_approved'] ?></td>
<td><?= $li['status'] ?></td>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target=".modal_<?= $li['lpf_no'] ?><?= $count ?>" data-backdrop="static" data-keyboard="false">Select</button>
</td>
</tr>
<?php } ?>
我尝试使用count从按钮生成它的唯一编号。我知道这是无意义的,因为每个按钮已经有一个唯一的 id。这是我的模态...
<div class="modal fade modal_<?= $li['lpf_no'] ?><?= $count ?>" tabindex="-1" role="dialog" aria-hidden="true" style="display: none;">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">In Progress...</h4>
</div>
<div class="modal-body">
<h4><?= $li['lpf_no'] ?></h4>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
而已。希望有人能帮助我这个。这是另一件事......我尝试将模态放入我的foreach
函数中。它有效,但模式破坏和页面设计也是如此。所以我更喜欢把它放在 foreach 函数之外。
解决方案
The problem is in your data-target
. You append the count on to the end of the data-target
in the loop (data-target=".modal_<?= $li['lpf_no'] ?><?= $count ?>
), along with your modal itself: <div class="modal fade modal_<?= $li['lpf_no'] ?><?= $count ?>
... </div>
. The problem is that $count
does not reflect the current count; the loop has already finished by this point; $count
now only refers to the last item.
Assuming you want a different modal for each item, you will indeed need to output the modals inside of a loop -- I'd actually recommend making use of a second loop for this, so as not to have to worry about things like closing your table:
<table>
<?php $count = 0; ?>
<?php foreach ($land_i as $li) { ?>
<?php $count++ ?>
<tr>
<td><?= $li['date_approved'] ?></td>
<td><?= $li['status'] ?></td>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target=".modal_<?= $li['lpf_no'] ?><?= $count ?>" data-backdrop="static" data-keyboard="false">Select</button>
</td>
</tr>
<?php } ?>
</table>
<?php $count = 0; ?>
<?php foreach ($land_i as $li) { ?>
<?php $count++ ?>
<div class="modal fade modal_<?= $li['lpf_no'] ?><?= $count ?>" tabindex="-1" role="dialog" aria-hidden="true" style="display: none;">
...
</div>
<?php } ?>
This way you will end up with a different modal for each item, and they will all have the correct data-target
reference. It also shouldn't break your formatting due to the closure of the <table>
before starting the second loop.
推荐阅读
- r - 使用哪个包在 R 中显示自定义表/信息
- .net - 实体框架一张表如何在同一张表中有两个外键?
- excel - 如何在用户窗体的 Web 浏览器控件中加载嵌入对象?
- python - jupyter notebook 缩放效果无法正常工作
- vba - 我正在使用此代码创建 PDF,在我的计算机上运行良好,但在其他计算机上存在兼容性问题。你以前遇到过这个吗?
- javascript - D3.js JSON.parse 意外字符
- python - Python:何时在实例化对象上使用类混合
- python - 对 Pandas 数据框的 Web 服务查询
- javascript - 不满足条件时显示模态
- voila - 瞧,jupyter notebook - 找不到模板