【发布时间】:2012-12-09 00:19:33
【问题描述】:
我有一个看起来像这样的模板。
示例模板
<div id="form-entry">
<h4>Some heading here</h4>
<div class="form-field">
<label>Some Label</label>
<input type="text" class="some_text" />
</div>
<div class="form-field">
<label>Some Label</label>
<input type="text" class="some_text" />
</div>
<div class="form-field">
<label>Some Label</label>
<input type="text" class="some_text" />
</div>
<div class="form-field">
<input type="checkbox" />
<label>Some Label</label>
</div>
<div class="form-field">
<label>Some Label</label>
<textarea></textarea>
</div>
</div>
如果用户选中复选框,我想删除(或隐藏)包含 checkbox 输入的 div 上方的 form-field div。 HTML 是自动呈现的,没有任何特定的id's 或classes。我该怎么做。 jQuery 的位置会有用吗?
JS
$('.form-field input[type=checkbox]').click( function(){
entity = $(this);
dad = entity.parent(); // This gets the form-field div element that contains the checkbox
});
如何捕获dad 元素上方的form-field div 元素?
【问题讨论】:
-
你不能在复选框前的框中添加一个 id 吗?抱歉,请重新阅读您的问题。你说你不能
-
entity.parent().parent(); ?
-
当我提到模板是自动呈现的,我真的不能。我也忘了提到我在同一页面上也有一些类似的
div.form-entry