javascript - 通过 php 动态创建的具有不同 id 的模型不起作用,但如果我将 id 写为“mymodel”而不是“mymodel [$id]”,则工作正常
问题描述
该模型对于每个 while 循环调用都有一个唯一的 id,因为 id 是随机生成的。对于 while 循环的每次调用,按钮都具有唯一的 id 并且正在调用唯一的模型。但是模型没有通过单击按钮显示。如果我排除为每个模型和按钮提供唯一 id 并给它们一个公共 id,则该模型会显示出来,但在这种情况下,它会为每个按钮单击打开相同的模型。
"<div class='modal fade' id='myModal[$id]' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
<div class='modal-dialog'>
<div class='modal-content'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal' aria-hidden='true'></button>
<h4 class='modal-title' id='myModalLabel'>More About $name</h4>
</div>
<div class='modal-body'>
<center>
<img src='img.jpg' name='dogimage' width='140' height='140' border='0' class='img-circle'>
<h3 class='media-heading'>$name</h3>
<hr>
<center>
<p class='text-left'><strong>Age(in months): </strong> $age
</p>
<p class='text-left'><strong>Weight(in kg): </strong>
$weight.</p>
<p class='text-left'><strong>Breed: </strong>
$breed.</p>
<p class='text-left'><strong>Gender: </strong>
$gender.</p>
</center>
</div>
<div class='modal-footer'>
<center>
<button type='button' class='btn btn-default' data-dismiss='modal'>Know enough about $name</button>
</center>
</div>
</div>
</div>
</div>
<div class='container'>
<div class='row'>
<div class='col-md-offset-2 col-md-8 col-lg-offset-3 col-lg-6'>
<div class='well profile'>
<div class='col-xs-12'>
<div class='col-xs-12 col-sm-8'>
<div id='dogrequestmessage'></div>
<h2>$name</h2>
<p><strong>Breed: </strong> $breed </p>
<p><strong>Weight(in kg): </strong> $weight </p>
<p><strong>Gender: </strong> $gender </p>
<p><strong>Age:(in months): </strong> $age </p>
</div>
</div>
<div class='col-xs-12 divider text-center'>
<div class='col-xs-12 col-sm-4 emphasis'>
<button class='btn btn-info btn-block' href='#aboutModal' data-toggle='modal' data-target='#myModal[$id]' name = 'viewprofile[$id]' id= 'view_profile'><span class='fa fa-user'></span> View Profile </button>
</div>
</div>
</div>
</div>
</div>
</div>"
解决方案
"<div class='modal fade' id='myModal[$id]'
问题在于 ID 值中使用的方括号。这些需要转义,以创建有效的 CSS 选择器,但用于该功能的库可能不会这样做。
请改用 ID 格式modal_$id
。假设 $id 值是数字,这将为您提供可以在 CSS 选择器中使用的 ID,而无需任何额外的转义。
推荐阅读
- python - 房间和物品字典问题,基于 Python 文本的游戏
- pandas - 如何在 Pandas DataFrame 中格式化单列?
- python - Apscheduler 时钟进程每次都崩溃
- pine-script - 将具有不同时间范围的新条件添加到当前脚本
- javascript - 为什么这个 javascript 不返回预期的代码?
- python - 使用 Python 标记 CSV 列
- r - 在 R 中的函数中提供数据和变量名称
- sonos - SONOS API 问题:AudioClip 可以排队多个剪辑吗?
- javascript - JavaScript Promise 返回 Promise 对象而不是字符串
- nginx - 无法从远程 PC 访问 GitBucket