我有一个表td
,其中包含一个textarea
和一个按钮,我想通过AJAX在按钮单击时发送textarea的值,但是选择时出现问题距离按钮最近的文本区域。
JavaScript
$(document).ready(function () {
$(document).on("click", ".addR", function () {
paperID = $(this).attr("paperID");
commentID = $(this).attr("commentID");
text = $(this).closest("textarea").val();
$.ajax({
data: {
paperID: paperID,
commentID: commentID,
text: text
},
type: 'POST',
url: 'add_rebuttal.php',
success: function (response) {
alert(response);
window.location.href = window.location.href;
}
});
});
});
PHP:
while ($row = mysql_fetch_assoc($comments)) {
echo "<tr><td>{$row['text']}</td>";
?>
<td><br /><textarea class="reText" rows='5' name='reText' id='reText' style='width:98%;' type='text'></textarea>
<button commentID="<?php echo $row['comment_id'] ?>" paperID="<?php echo $paper_id ?>" class="addR" type="button" name="addR" id="addR">send rebuttal</button></td></tr> <?
}
问题是 $(this).closest("textarea").val();
返回未定义,那么我该如何解决这个问题?
最佳答案
closest() 返回最近的祖先。您的文本区域不是按钮的祖先,而是 previous sibling 。相反,请尝试:
text = $("textarea", $(this).parent()).val();
关于php - 选择距离按钮最近的文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11496995/