如何使用 jQuery 获取文本区域父级

发布于 2024-12-20 13:32:13 字数 803 浏览 2 评论 0原文

我遇到这种情况:

<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'>&nbsp;
      <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森林很绿却致人迷途 2024-12-27 13:32:13

尝试:

$(this).parents(".postit_popup").find("textarea").val();

或者,如果 .postit_popup 中有多个文本区域,则使用:

$(this).parents(".postit_popup").find(".txtNota").val();

这样做的作用是从 .okNota 向上遍历 DOM,直到 .postit_popup找到 >,然后返回 DOM 以查找 textarea 或具有 .txtNota 类的元素。

Try:

$(this).parents(".postit_popup").find("textarea").val();

Or if you have more than one textarea in .postit_popup then use:

$(this).parents(".postit_popup").find(".txtNota").val();

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 a textarea or an element with the .txtNota class.

沉默的熊 2024-12-27 13:32:13

这有帮助吗: http://jsfiddle.net/vgtQR/

编辑(对于 JohnP)

HTML

<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'>
       <div class='okNota'>OK Image</div>
   </div>
</div>

JS

$('.okNota').click( function() {
   alert($('.txtNota').val());
});

Does this help: http://jsfiddle.net/vgtQR/

EDIT (for JohnP)

HTML

<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'>
       <div class='okNota'>OK Image</div>
   </div>
</div>

JS

$('.okNota').click( function() {
   alert($('.txtNota').val());
});
紫﹏色ふ单纯 2024-12-27 13:32:13
$(".okNota").click( function() {     
var value = $('.txtNota').val();
 }); 

这会起作用。您缺少“=”。

<textarea class = 'txtNota'>some text</textarea> 
$(".okNota").click( function() {     
var value = $('.txtNota').val();
 }); 

This will work. You are missing '='.

<textarea class = 'txtNota'>some text</textarea> 
能否归途做我良人 2024-12-27 13:32:13

使用:

$(this).closest(".postit_popup").find("textarea").val();

Use:

$(this).closest(".postit_popup").find("textarea").val();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文