dsdv76767671 2013-09-23 11:32
浏览 46

单击链接时勾选复选框 - jquery

I am using the following script to create an image gallery, people click the checkbox under the images they want and then submit them to me. This works fine, but I have now added Jquery Fancybox which opens the larger version of the image into a lightbox. The script then creates a link under the lightbox image "Select This Image". What I am trying to do is when the user clicks on that link it then checks / unchecks (if the user presses the link again) the select box under the image on the main page. I cant seem to get it to work though, this is what I have :

This is the Jquery :

        <script>
        $(document).ready(function() {
        $(".fancybox-thumb").fancybox({
             afterLoad: function() {
            this.title = '<a class="selectlink" href="#" id="' + this.title + '">Select This Image</a> ';
        },
            prevEffect  : 'none',
            nextEffect  : 'none',
            helpers : {

                title   : {
                    type: 'outside'
                },
                thumbs  : {
                    width   : 50,
                    height  : 50
                }
            }

        });
    });

    $(function () {
    $('#selectlink').click(function () {
        $('.imagewrapper').find(this.id).attr('checked', this.checked);
    });
    });
    </script>

This is the html / php part :

    $sql2 = <<<SQL
        SELECT *
        FROM `albums`
        WHERE albumid = '$albumid' AND isalbum = ''
    SQL;


    if(!$result2 = $db->query($sql2)){
        die('There was an error running the query [' . $db->error . ']');
    }
    while($row2 = $result2->fetch_assoc()){

        echo '<div class="imagewrapper">';
        echo '<a title="'.$row2['image'].'" class="fancybox-thumb" rel="fancybox-thumb" href="albums/'.$albumid.'/800x600/'.$row2['image'].'"><img style="border-radius:5px;" src="albums/'.$albumid.'/thumbnails/'.$row2['image'].'" /></a>';
          echo '<div style="text-align:center;">';
          echo '<strong>Select : </strong><input type="checkbox" name="'.$row2['image'].'" id="'.$row2['image'].'" />';
          echo '</div>';
          echo '</div>';
        }

What is happening is the link is being created in the lightbox popup just fine and the title of the link is the file name e.g 123456.jpg , but when I click the link, the checkbox with the id of 123456.jpg doesnt get ticked ?.

  • 写回答

1条回答 默认 最新

  • dongweng6241 2013-09-23 11:38
    关注

    You could try to set a variable first, as this.id may refer to $('.imagewrapper').

    $('#selectlink').click(function () {
        $('.imagewrapper').find(this.id).attr('checked', this.checked);
    });
    

    would become:

    $('#selectlink').click(function () {
        var myId = $(this).find(this.id);
        $('.imagewrapper #' + myId).attr('checked', this.checked);
    });
    

    i cant test it right now, but it may work.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题