隐藏 div 中的 PHP SRC 值更改



我正在尝试获取pdf/image的文件名以在本地表中显示它。当我得到文件名时,我希望它位于一个<a>标签中,然后 href 到其中隐藏的弹出div 是<embed>.问题是 src 的值没有改变,所有文件名都相同(它都取名字)

这是我的代码:

while($row=mysql_fetch_assoc($search)){
?>
<div aria-hidden="true" role="dialog" tabindex="-1" id="login-forms" class="modal leread-modal fade in">
<div class="modal-dialog">
<div class="modal-header">
<button aria-label="Close" data-dismiss="modal" class="close" type="button"><span aria-hidden="true">X</span>
</button>
</div>
<div class="modal-body" name="place">
<embed src="stuUploads/<?php echo $row['upload'];?>" id="up" type="application/pdf" width="600px" height="500px" />
</div>
<div class="modal-footer footer-box">
</div>
</div>
</div>

我使用 jQuery 更改了<embed>标签中 src 属性的值,但结果是文件的姓氏(数据库中的最后一行)

这是代码

<script>
$(document).ready(function(){                                                
$('#up').attr('src',"stuUploads/<?php echo $row['upload'];?>");
});
</script>

这是整个循环

if(mysql_num_rows($search)>0){ ?>
<div class="container-table10">
<div class="wrap-table100">
<div class="table100">
<table>
<thead>
<tr class="table100-head">
<th class="column1">appeal date</th>
<th class="column2">course code</th>
<th class="column3">date of exam </th>
<th class="column4">reason</th>
<th class="column5">course title</th>
<th class="column6">AY</th>
<th class="column7">semester</th>
<th class="column8">student id</th>
<th class="column8">Lecturer
<br>Name</th>
<th class="column9">upload</th>
<th class="column9">Eligibility</th>
</tr>
</thead>
<tbody>
<?php
while($row=mysql_fetch_assoc($search)){
?>
<div aria-hidden="true" role="dialog" tabindex="-1" id="login-forms" class="modal leread-modal fade in">
<div class="modal-dialog">
<div class="modal-header">
<button aria-label="Close" data-dismiss="modal" class="close" type="button"><span aria-hidden="true">X</span></button>
</div>
<div class="modal-body" name="place">
<embed src="stuUploads/<?php echo $row['upload'];?>" class="up" type="application/pdf" width="600px" height="500px" />
<script>
$(document).ready(function() {
$('.up').attr('src', "stuUploads/<?php echo $row['upload'];?>");
});
</script>
</div>
<div class="modal-footer footer-box">
</div>
</div>
</div>
<tr>
<td class="column1">
<?php echo $row['appealm_date'];?>
</td>
<td class="column2">
<?php echo $row['course_code'];?>
</td>
<td class="column3">
<?php echo $row['date_of_exam'];?>
</td>
<td class="column4"><a class="login modal-form" data-target="#login-form" data-toggle="modal" href="#">show</a></td>
<td class="column5">
<?php echo $row['course_title'];?>
</td>
<td class="column6">
<?php echo $row['appealm_ay'];?>
</td>
<td class="column7">
<?php echo $row['appealm_sem'];?>
</td>
<td class="column8">
<?php echo $row['student_id'];?>
</td>
<td class="column8">
<?php echo $row['lecturer_name'];?>
</td>
<td class="column9">
<a class="login modal-form" id="aaa" data-toggle="modal" href="#login-forms">
<?php echo $row['upload'];?>
</a>
</td>
<td class="column8">
<input type="checkbox" name="eli" id="">
</td>
</tr>
<div aria-hidden="false" role="dialog" tabindex="-1" id="login-form" class="modal leread-modal fade in">
<div class="modal-dialog">
<div id="login-content" class="modal-content">
<div class="modal-header">
<button aria-label="Close" data-dismiss="modal" class="close" type="button"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Reason</h4>
</div>
<div class="modal-body">
<p class="p">
<?php echo $row['appealm_reason'];?>
</p>
</div>
<div class="modal-footer footer-box">
</div>
</div>
</div>
</div>
<?php   
}
?>
</tbody>
</table>
</div>
</div>
</div>
<?php   
}}
}}
?>

如果有其他方法可以让文件在同一页面中弹出,请提出建议。 感谢任何帮助。

使用class而不是id。Id 是唯一的

<embed src="stuUploads/<?php echo $row['upload'];?>" class="up" type="application/pdf" width="600px" height="500px" /> 

并将相应的 js 元素id更改为class

$(document).ready(function(){
$('.up').attr('src',"stuUploads/<?php echo $row['upload'];?>");
});

使用 class 属性而不是 id。ID 应该是唯一的(这意味着只使用一次)。

您只能在 while 循环中访问$row[],因为您正在循环$search这是一个多维数组。

在你的
  1. 循环中使用<a href="#login-forms" data-toggle="modal" data-target="#login-forms" data-url="stuUploads/<?php echo $row['upload'];?>">XXX</a>

  2. 如果您使用的是引导程序,请添加

$('#login-forms').on('shown.bs.modal', function (e) { var $invoker = $(e.relatedTarget); $("#up").attr("src", $invoker.data("url")); });

请记住,$row['upload']只是一个值,每次通过while循环都会更改。 当您将up从 id 更改为类时,您使脚本实质上重新定义了每次运行时页面上已有的每个upsrc。 更好的解决方案是定义一个更好的id,因为您只打算更改单个embed。 是否可以使用$row['upload']作为您的身份证件?

<embed src="stuUploads/<?php echo $row['upload'];?>" id="<?php echo $row['upload'];?>" type="application/pdf" width="600px" height="500px" /> 

然后,您可以将脚本更改为:

<script>
$(document).ready(function(){
$('#'+<?php echo $row['upload'];?>).attr('src',"stuUploads/<?php echo $row['upload'];?>");
});
</script>

最新更新