【问题标题】:JQuery : Load a file in modal windowJQuery:在模式窗口中加载文件
【发布时间】:2017-05-18 11:54:29
【问题描述】:

我在像这样点击图片时得到一个文件名

<a href="#"  class="aclick" >
   <img data-link="petroliumjelly.php" src="productsimgs/petroliumjelly.png" class="img-responsive" id="2">
</a>

现在这里弹出一个模态窗口,我有页面链接 petroliumjelly.php 我想在该模态窗口中打开。

  <script type="text/javascript" language="javascript">
       $(".aclick").click(function (e) {
        e.preventDefault();
        var id = $(this).attr('id');
        var link = $(this).data("data-link");
        console.log(id);
        console.log(link);
        $('#modal_'+id).modal('show');
        $page = $(this).find('img').attr("data-link"); 
        //$('.modal-body')// load page her 
    });

</script> 

如何在模态窗口中加载该页面,请帮助。

【问题讨论】:

标签: javascript jquery html twitter-bootstrap


【解决方案1】:

.aclick 没有iddata-link,它们会返回undefined,您需要找到图像并从图像中获取。要获得data-link,请使用().data('link') 而不是data('data-link')。要在模态中加载文件,您必须使用 AJAX.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);
        $('#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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多