im trying to get the parent of this input but it just wont, i get the label at its开发者_StackOverflow most. what im i doing wrong?
<div class="input req">
<p>
<label>
<%= Html.TextBoxFor(model => model.Street, new { TABINDEX = 1,data_validate = "required|min[2]" })%>
</label>
</p>
<p class="msg">
</p>
</div>
JQUERY
alert($('#Street').parent('div.input').length);
EDIT: i should add this is all work in a thickbox and i get length 1 if i take parent('label')
EDIT2: missed this link that is posted How to find a parent with a known class in JQuery?
-2 minus for what?
alert($('#Street').parents('div.input:first').length);
which is similar to :
alert($('#Street').closest('div.input').length);
parent just look for the immediate parent. You should use one of the above methods instead
alert($('#Street').parents('div.input').length);
^
parent()
will go up one level, on the other hand .parents()
will go up until it matches the selector.
In your case, the immediate parent is the element p
, not the div.input
.
try
alert($('#Street').closest('div.input').length);
try
alert($('#Street').parent('label').parent('p').parent('div.input').length);
Parent only looks at the element directy above it, this way you take the steps necessary to climb the DOM tree.
精彩评论