Jquery 拆下上面的div

Jquery 拆下上面的div,jquery,html,hide,Jquery,Html,Hide,我有一个类似这样的模板 样本模板 如何捕获dad元素上方的表单字段div元素?您可以这样做 $('#form-entry input[type="checkbox"]').change(function(){ $(this).parent().prev().remove(); }); 删除包含复选框的.form\u字段上方的.form\u字段,如果选中了该复选框?您可以使用以下jQuery: $('.form-field input[type=checkbox]').click( f

我有一个类似这样的模板

样本模板 如何捕获
dad
元素上方的
表单字段
div元素?

您可以这样做

$('#form-entry input[type="checkbox"]').change(function(){
    $(this).parent().prev().remove();
});


删除包含复选框的
.form\u字段
上方的
.form\u字段
,如果选中了该复选框?

您可以使用以下jQuery:

$('.form-field input[type=checkbox]').click( function(){
    entity = $(this);
    dad = entity.parent().prev().remove(); // This gets the form-field div element that contains the checkbox
}); 
$('.form-field input[type=checkbox]').click(function() {
    $(this).parent().prev().toggle(); // parent is the checkboxs div, prev returns the previous element in the DOM
});​
切换
将在每次单击复选框时隐藏/显示上一个div


下面是一个示例,说明了这是如何工作的

您不能在复选框前面添加一个id吗?对不起,请重读你的问题。你说你不能做父母吗?因为我知道模板是自动呈现的,所以我真的不能。我也忘了提一下,我在同一个页面上有几个类似的<代码> DIV.FECT-Eng/<代码>,不需要检查它是否被检查:如果它从最初的(未选中的)状态改变,它必须被检查。@ DistRoy——是的,你可能是对的,我确实考虑过了,但是支票并不重要,并展示了如何检查它,如果初始状态更改为checked或类似的东西。我不确定我是否喜欢gdoron的最后一次编辑。阿德内奥的版本对我来说很好。
$('.form-field input[type=checkbox]').on('change', function(){
    if (this.checked) 
        $(this).closest('.form-field').prev().remove();
}); 
$('.form-field input[type=checkbox]').click( function(){
    entity = $(this);
    dad = entity.parent().prev().remove(); // This gets the form-field div element that contains the checkbox
}); 
$('.form-field input[type=checkbox]').click(function() {
    $(this).parent().prev().toggle(); // parent is the checkboxs div, prev returns the previous element in the DOM
});​