2013-09-23 96 views
0

我使用以下脚本创建图片库,人们点击他们想要的图片下的复选框,然后将它们提交给我。这工作正常,但我现在已经添加了Jquery Fancybox,它将更大版本的图像打开到lightbox中。该脚本然后在lightbox图片“选择此图片”下创建一个链接。我想要做的是,当用户点击该链接时,它会检查/取消选中(如果用户再次按下链接)主页上图像下方的选择框。我似乎无法得到它的工作,虽然,这是我有:点击链接时勾选复选框 - jquery

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

这是HTML/PHP的一部分:

$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>'; 
     } 

发生了什么是在Lightbox弹出窗口中创建的链接就好了,链接的标题是文件名,例如123456.jpg,但是当我点击链接时,ID为123456.jpg的复选框不会被打勾?

+2

使用'.prop()'而不是'.attr()' –

回答

0

您可以尝试先设置一个变量,因为this.id可能引用$('。imagewrapper')。

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

将成为:

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

我不能马上测试,但它可能工作。

+0

这似乎不起作用,复选框和链接都有相同的ID它应该在理论上做? –