每个复选框都会触发事件

我有几个图像,旁边是复选框,它不会单独显示,但如果单击标签触发复选框true或false,现在我认为一切都很好,直到我必须使用this所以每个复选框响应对于它的元素,我的代码运行但是当选中一个复选框时,它会触发每个图像的所有事件。

  $(".suggestions").click(function() { if ($(".checkbox").prop('checked') === true) { $("h2").fadeIn("fast").fadeOut(5000) } if ($(".checkbox").prop('checked') === false) { $("h2").fadeIn("fast").fadeOut(5000) } }) 

基本上,单击复选框时的代码显示已添加的消息,当取消选中时,会显示已删除的消息并消失。 我怎样才能使它工作,所以它使用this ? 这是我的小提琴

这适用于带有类型复选框的每个输入,如果您也保留此结构。

 $('input:checkbox').on('change', function() { if ($(this).prop('checked') === true) { $(this).parent('div').siblings('.flex-column').children('h2.addMsg').fadeIn("fast").fadeOut(5000); } else { $(this).parent('div').siblings('.flex-column').children('h2.removeMsg').fadeIn("fast").fadeOut(5000); } }); 

如果您有任何疑问,请告诉我。

编辑:

当然你可以用classname kdng替换input:checkbox