如何使用 jQuery 获取文本区域父级
我遇到这种情况:
<div class='postit_popup' id='xxx'>
<div style='margin-left:20px;margin-top:20px;text-align:justify;'>
<textarea class'txtNota'>some text</textarea>
</div>
<div class='clear'></div>
<div style='margin-left:20px;' align='left'>
<img class='okNota' src='img/ico/task-completed.png' height='20' width='20'>
<img class='cancewlNota' src='img/ico/button_cancel.png' height='20' width='20'>
</div>
</div>
当单击“okNota”类的图像时,我需要获取文本区域中的内容。
我是这样做的:
$(".okNota").click( function() {
var obj = $(this).parent().parent();
alert(obj.children(0).children(0).val());
});
但我需要知道是否还有其他方法来获取它。
非常感谢。
I have this situation:
<div class='postit_popup' id='xxx'>
<div style='margin-left:20px;margin-top:20px;text-align:justify;'>
<textarea class'txtNota'>some text</textarea>
</div>
<div class='clear'></div>
<div style='margin-left:20px;' align='left'>
<img class='okNota' src='img/ico/task-completed.png' height='20' width='20'>
<img class='cancewlNota' src='img/ico/button_cancel.png' height='20' width='20'>
</div>
</div>
I need to get content in the textarea when there is a click on the image with class "okNota".
I did it in this way:
$(".okNota").click( function() {
var obj = $(this).parent().parent();
alert(obj.children(0).children(0).val());
});
But I need to know if there is another way to obtain it.
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试:
或者,如果
.postit_popup
中有多个文本区域,则使用:这样做的作用是从
.okNota
向上遍历 DOM,直到.postit_popup
找到 >,然后返回 DOM 以查找textarea
或具有.txtNota
类的元素。Try:
Or if you have more than one textarea in
.postit_popup
then use:What this does is traverse up the DOM from
.okNota
until.postit_popup
is found and then it goes back down the DOM to find atextarea
or an element with the.txtNota
class.这有帮助吗: http://jsfiddle.net/vgtQR/
编辑(对于 JohnP)
HTML
JS
Does this help: http://jsfiddle.net/vgtQR/
EDIT (for JohnP)
HTML
JS
这会起作用。您缺少“=”。
This will work. You are missing '='.
使用:
Use: