Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...