php - 选择距离按钮最近的文本区域

标签 php javascript jquery closest

我有一个表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/

相关文章:

php - 如何使用表格中的按钮删除特定行的数据?

php - Firefox 中的 JQuery SYNC Ajax 调用错误

php - 第一次尝试无法登录

php - 使用移动网页检测 Android 应用程序是否已安装在设备上 - PHP 和 JS

javascript - super 幻灯片将图像大小调整为大

jquery - 单击类时更改样式元素

php - 这是事件日志的实用概念吗?

javascript - 具有多个类的 jQuery 条件逻辑

javascript - 如何在javascript中将字符串方程式转换为数字?

jquery - 如何将jquery函数应用到每个实例