我有這組表單,當用戶檢查輸入時,我需要獲取該div以外的值,基本上是一個父div,嘗試它它使用.parent(),然後找到,但不能使它正常工作,不知道做什麼我做錯了的事,我有一個js http://jsfiddle.net/creativestudio/bCgeD/2/Jquery在檢查子複選框時獲取父元素輸入的值
這是我的js:
function set_index_id_checkbox() {
$("input:checkbox[name='like']").each(function(index){
var currElem = $(this);
var prevLabel = currElem.next();
currElem.attr("id", this.id + index);
prevLabel.attr("for", prevLabel.attr("for") + index);
});
}
set_index_id_checkbox();
function getValues(){
$("input:checkbox[name='like']").click(function(){
if (this.checked) {
$(this).closest('.reply-form').find('.feedback_nid').text(this.value);
} else {
// ...
}
});
} getValues();
這是我的html:
<input type="hidden" class="feedback_nid" value="form a 111">
<input type="hidden" class="branch_nid" value="form a 222">
<div class="block">
<!-- value -->
<span class="info-item float-left value">
<span class="likes">value</span>
</span><!-- /.value -->
<!-- checkbox -->
<div class="btn like-checkbox float-right">
<input id="like" class="like" type="checkbox" name="like" value="33">
<label class="like" for="like">like 1</label>
</div><!-- /checkbox -->
</div>
</div>
<div class="reply-form">
<input type="hidden" class="feedback_nid" value="form a 111">
<input type="hidden" class="branch_nid" value="form a 222">
<div class="block">
<!-- value -->
<span class="info-item float-left value">
<span class="likes">value</span>
</span><!-- /.value -->
<!-- checkbox -->
<div class="btn like-checkbox float-right">
<input id="like" class="like" type="checkbox" name="like" value="45">
<label class="like" for="like">like 2</label>
</div><!-- /checkbox -->
</div>
</div>
<div class="reply-form">
<input type="hidden" class="feedback_nid" value="form a 111">
<input type="hidden" class="branch_nid" value="form a 222">
<div class="block">
<!-- value -->
<span class="info-item float-left value">
<span class="likes">value</span>
</span><!-- /.value -->
<!-- checkbox -->
<div class="btn like-checkbox float-right">
<input id="like" class="like" type="checkbox" name="like" value="66">
<label class="like" for="like">like 3</label>
</div><!-- /checkbox -->
</div>
</div>
您也可以使用父母關閉兩次parent() – Perry