php src值更改在隐藏的div中

时间:2019-03-13 13:35:43

标签: javascript php jquery

我正在尝试获取pdf / image的文件名,以将其显示在本地表中。当我得到文件名时,我希望它在<a>标记中,然后在其中隐藏到弹出弹出div的href为<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   
   }}
 }}
?>

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

4 个答案:

答案 0 :(得分:0)

使用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'];?>");
 });

答案 1 :(得分:0)

使用class属性代替id。一个ID应该是唯一的(这意味着只能使用一次)。

由于您要遍历多维数组$row[],因此只能在while循环内访问$search

答案 2 :(得分:0)

  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")); });

答案 3 :(得分:0)

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

 <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>