dongrong1856 2019-01-22 21:04
浏览 68
已采纳

如何使用ajax在bootstrap模式中加载某种类型的文件

I have a simple bootstrap modal here: https://jsfiddle.net/blacode/kecyrv4q/
I´m trying to do the same but in a remote way with ajax. I've added a bit more of complexity adding glob function (PHP) to look for an image in a specific directory with image type validation in ajax request. Here is the code:
index.php

    <body>
        <div class="container">
            <h2>Basic Modal Example</h2>

            <!-- Trigger the modal -->
            <a href="image.php" class="modalImage" data-toggle="modal" data-var="images/image/">
                <img class="img-fluid" src="image.png" alt="" style="width:20%">
            </a>

            <!-- Modal -->
            <div class="modal fade" id="myModal" role="dialog">
                <div class="modal-dialog modal-lg">

                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Modal Header</h4>
                        </div>
                        <div class="modal-body">
                            <img class="img-fluid img-responsive" src="" alt="">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                    <!-- Close modal content-->

                </div>
            </div>
            <!-- Close modal -->

        </div>
        <!-- Close container -->

    </body>  

page to search image in specific directory (image.php):

<?php
header('Content-Type: application/json');
$directory = $_POST['var'];
echo json_encode (glob($directory. '*.{png,mp4}', GLOB_BRACE));
?>  

ajax request:

$(document).ready(function(){
$('.modalImage').click(function (event) {
event.preventDefault();
var data = this.dataset;

        $.ajax({
            url: "image.php",
            data: data,
            type: "POST",
            dataType: "json",
            success: function(data) {
                var imgs = data.map(function(img) {
                    var html = "";
                    var fileExtension = img.substring(img.lastIndexOf('.'));
                    if (fileExtension == ".png") {
                        return html = '<img src="'+img+'"></img>';
                        $('.modal-body').load(html,function(){
                            $('#myModal').modal({show:true});
                        });
                    }
                });
            } 
        });
    });
});    

Unfortunately the page doesn't respond. The console shows these warnings:

unreachable code after return statement  
ReferenceError: $ is not defined  

Is there a way to do that?

  • 写回答

1条回答 默认 最新

  • dpvmtdu364462 2019-01-22 21:29
    关注

    You are returning from the function before trying to set the html and showing the modal. Just set the value of html and then the html of the .modal-body element like below.

    As for the issue with $ undefined, you need to reference jQuery prior to this script.

    <script src="https://code.jquery.com/jquery-3.3.1.js" 
            integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" 
            crossorigin="anonymous"></script>
    <script>
        $(document).ready(function(){
            $('.modalImage').click(function (event) {
                event.preventDefault();
                var data = this.dataset;
    
                $.ajax({
                    url: "image.php",
                    data: data,
                    type: "POST",
                    dataType: "json",
                    success: function(data) {
                        var imgs = data.map(function(img) {
                            var html = "";
                            var fileExtension = img.substring(img.lastIndexOf('.'));
                            if (fileExtension == ".png") {
                                html = '<img src="'+img+'"></img>';
                                $('.modal-body').html(html);
                                $('#myModal').modal({show:true});
    
                            }
                        });
                    } 
                });
            });
        });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)