[英]JQuery Closest Class Select
I have a layout as follows (simplified) 我的布局如下(简化)
<div>
<div class="msgOutput" id="msg2"></div>
<form>
<table>
<tbody>
<tr>
<td>
<p><input type="file" class="documentFile" id="documentFile2" /></p>
</td>
</tr>
... more code here ...
</tbody>
</table>
</form>
</div>
And I want to put something in the div.msgOutput
when the input.documentFile
changes (There are multiple copies on the page) 我想把东西在div.msgOutput
当input.documentFile
变化(有在页面上的多个副本)
There is more code, so I do definitely need to search for the closest class="msgOutput"
还有更多代码,因此我确实需要搜索最接近的class="msgOutput"
I know it's something to do with closest()
or parent()
or something, but it's not working :/ 我知道这与closest()
或parent()
东西有关,但不起作用:/
So far I've tried things like: 到目前为止,我已经尝试过类似的事情:
$('.documentFile').change(function(e){
....
$($(this).attr('id')).parent().children('.msgOutput').attr('id').append('Hello World');
$(this).closest('div').find('.msgOutput').append('Hello World');
$(this).closest('div').sibling('.msgOutput').append('Hello World');
....
});
But it's not working :(. Anyone have any ideas? 但这不起作用:(。任何人有任何想法吗?
I would recommend something like this: http://jsfiddle.net/cr94dtk0/ 我会推荐这样的东西: http : //jsfiddle.net/cr94dtk0/
You should put a class or id around the containing div, so that you can do some DOM Traversal 您应该在包含的div周围放置一个类或id,以便可以进行DOM遍历
<div class="container">
<div class="msgOutput" id="msg2">hi</div>
<form>
<table>
<tbody>
<tr>
<td>
<p><input type="file" class="documentFile" id="documentFile2" /></p>
</td>
</tr>
</tbody>
</table>
</form>
$(".documentFile").change(function(){
$(this).closest(".container").find(".msgOutput").text("TEXT GOES HERE");
});
You've already found the closest div but you're adding extra selector methods which is screwing your result. 您已经找到了最接近的div,但是要添加额外的选择器方法,这会使您的结果更加混乱。
This works: 这有效:
$(this).closest('div').append('Hello World');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.