Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

javascript - JQuery : Load a file in modal window

I am getting a file name on image click like this

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

Now there pops up a modal window here and i have the page link petroliumjelly.php that i want to open in that modal window .

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

How can i load that page in modal window please help .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

Or use modal.load() to load the 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);

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...