且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery:在模式窗口中加载文件

更新时间:2022-05-15 15:31:40

.aclick没有iddata-link,它们将返回undefined,您需要查找图像并获取这些图像从图像.要获取data-link,请使用().data('link')而不是data('data-link').要以模式加载文件,您必须使用 AJAX 整理文件.

The .aclick doesn't have an id and data-link, they will return undefined, you need to find the image and get those from the image. To get the data-link use ().data('link') not data('data-link'). To load the file in your modal you'll have to use AJAX or the .load() to laod a file.

<script>
    $(".aclick").click(function (e) {
        e.preventDefault();
        $image = $(this).find('img');
        var id = $($image).attr('id');
        var link = $($image).data("link");
        console.log(id);
        console.log(link);
        $('#modal_'+id).modal('show');

        // using AJAX to fetch the file
        $.get(link, function (response) {
            $('.modal-body').html(response);
        })
    });
</script>

或使用modal.load()加载文件.

<script>
    $(".aclick").click(function (e) {
        e.preventDefault();
        $image = $(this).find('img');
        var id = $($image).attr('id');
        var link = $($image).data("link");
        console.log(id);
        console.log(link);

        // load the file
        $('.modal-body').load(link, function () {
            $('#modal_'+id).modal('show');
        })
    });
</script>