我有textarea的表单,允许用户添加回复
我想在每个回复中添加按钮(类" quoteMsg")。单击它将复制回复内容(具有class =" replyMsg")+作者姓名+日期(SPAN标记内)到textarea(name =" msgText")+ add& #34; $"注意内容之前和之后
以下是HTML示例:
<form method="post" action='index.php' id="submitReply">
<p><textarea name="msgText"></textarea> <span></span></p>
</form>
<section class="replyBox">
<article class="left">
<header>
<h2>Re: Voucher Release Prblm - Comm</h2>
<span>By esther <strong>»</strong> 21-10-2014 12:06</span>
<a href="#" class='quoteMsg'>Quote</a>
</header>
<div class="replyMsg">
If will happen again, I will open a new track<br />
</div>
</article>
</section>
<section class="replyBox">
<article class="left">
<header>
<h2>Re: Voucher Release Prblm - Comm</h2>
<span>By esther <strong>»</strong> 23-07-2014 11:19</span>
<a href="#" class='quoteMsg'>Quote</a>
</header>
<div class="replyMsg">
OK, thanks
</div>
</section>
我应该这样做:
<script>
$('.quoteMsg').click(function()
{
var msgContent = parents("article").find(".replyMsg").val();
//alert(msgContent);
})
</script>
? 如果是的话 - 我如何进入textarea字段?
答案 0 :(得分:2)
您可以使用jQuery的closest()
属性来查找这样的元素:
$(document).on('ready', function() {
$('.quoteMsg').click(function() {
var txt = $(this).closest('.replyBox').find('.replyMsg').text();
$("textarea[name='msgText']").val($.trim(txt));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" action='index.php' id="submitReply">
<p><textarea class="msgText" name="msgText"></textarea> <span></span></p>
</form>
<section class="replyBox">
<article class="left">
<header>
<h2>Re: Voucher Release Prblm - Comm</h2>
<span>By esther <strong>»</strong> 21-10-2014 12:06</span>
<a href="#" class='quoteMsg'>Quote</a>
</header>
<div class="replyMsg">
If will happen again, I will open a new track<br />
</div>
</article>
</section>
<section class="replyBox">
<article class="left">
<header>
<h2>Re: Voucher Release Prblm - Comm</h2>
<span>By esther <strong>»</strong> 23-07-2014 11:19</span>
<a href="#" class='quoteMsg'>Quote</a>
</header>
<div class="replyMsg">
OK, thanks
</div>
</article>
</section>
希望这有帮助!
答案 1 :(得分:0)
简单方法
$('.quoteMsg').click(function()
{
var msgContent = $(this).closest("article").find(".replyMsg").text();
console.log(msgContent);
});