[英]Tick a checkbox when a link is clicked - jquery
我正在使用以下脚本创建图像库,人们单击所需图像下方的复选框,然后将其提交给我。 效果很好,但是我现在添加了Jquery Fancybox,它将较大版本的图像打开到灯箱中。 然后,脚本会在灯箱图片“选择此图片”下创建一个链接。 我要尝试做的是,当用户单击该链接时,它选中/取消选中(如果用户再次按下该链接)主页上图像下方的选择框。 我似乎无法使它正常工作,这就是我所拥有的:
这是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的复选框没有被打勾? 。
您可以尝试先设置一个变量,因为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);
});
我现在无法对其进行测试,但是它可能会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.