查找最接近的对象类-Jquery
Jquery查找最接近的对象类-Jquery,jquery,html,select,closest,Jquery,Html,Select,Closest,Jquery var courseSelect = $(".courseSelected"); $(".courseSelected").click(function() { if($(this).attr('checked')) { var selects = $(this).parent().next(".select").attr('title'); alert(selects);
var courseSelect = $(".courseSelected");
$(".courseSelected").click(function()
{
if($(this).attr('checked'))
{
var selects = $(this).parent().next(".select").attr('title');
alert(selects);
}
});
HTML
<div>
<div style="float:left;">
<input name="course[]" type="checkbox" value="AUTOCAD" class="courseSelected" />
</div>
<div style="width:100px; float:left;">AUTOCAD</div>
<select name="fromto[]" class="select small" disabled="disabled" title="Batch">
<option value="">--Select--</option>
<option value="May 2012,June 2012">Autocad 1(11:30 AM)</option>
<option>Batch Name</option>
</select>
</div>
AUTOCAD
--挑选--
Autocad 1(上午11:30)
批次名称
通过选中复选框,我需要找到select元素,我尝试了使用NEXTER和parent()。next()也一样,但在这种情况下似乎不起作用
...
if($(this).attr('checked')) {
var selects = $(this).parent().parent().find(".select").attr('title');
alert(selects);
}
...
无论如何,为了简单起见,如果您控制了标记,我会这样更改您的标记
<input name="course[]" type="checkbox" ... data-related-select="yourselect" />
...
<select id="yourselect">
...
查找最接近的第一个对象类-Jquery
$(this).parent().parent().find('.show-all-result').first().show();
您的标记中的复选框在哪里对不起,我的错..它现在已更新..为什么底部有一个额外的
?为什么select
会这样缩进,它不是AUTOCAD div
的子项?很抱歉,它现在被编辑了。忘记复制整个标记了。。
var selects = document.getElementById($(this).data('related-select'));
$(".courseSelected").click(function(){
if(this.checked){
var selects = $(this)
.parent() // go to checkbox parent
.next() // jump to div have AUTOCAD
.next(".select") // to select box
.attr('title'); // retrieve the title
alert(selects);
}
});
$(this).parent().parent().find('.show-all-result').first().show();