javascript - 为什么更改文本区域会阻止使用 AJAX 将内容加载到其中?

标签 javascript jquery ajax

我正在为我网站上留下的评论创建一个报价系统。如果用户想回复别人所说的话,他或她可以单击评论上的“引用”按钮,这将运行一个脚本,将引用的评论(和一些格式)添加到唯一的 <textarea> 中。在页面上。该脚本还将考虑 <textarea> 中的任何先前内容。 ,在此其他内容之后添加引用的评论。

问题是如果我点击 <textarea> , 给它焦点,或者在我添加更多引用评论时尝试调整它的大小,即使我的“测试”警报运行,内容也不会加载;我必须刷新页面才能重置它。如果我避免更改它,我可以整天单击引用按钮,并适本地向 <textarea> 添加无限数量的评论。 .我是 Javascript 的新手,所以我不确定是什么导致了这个问题。

function quoteComment()
{
    $(".quote").on("click", function(event){

        event.preventDefault();

        var quoteID = this.id.match(/\d+/);
        var textContent = $("#textarea").val();

        $("#textarea").load("/php/_quote.comment.php", {

            id: quoteID,
            content: textContent

        }, function(){

           //$("#textarea").focus(); <-- this... 
           //resize code <-- or this (either alone or together) will break the functionality
           alert("test");
        });
    });
}

最佳答案

您不能使用 load()<textarea> 上.当您需要设置 value 时,它会尝试在内部设置 innerHTML相反。

尝试使用 $.post相反,并在回调中设置值

$.post("/php/_quote.comment.php", {
  id: quoteID,
  content: textContent
}, function(data) {
  $("#textarea").val(data).focus();
  alert("test");
}).fail(function(){ alert('failed for some reason, inspect in dev tools network');})

关于javascript - 为什么更改文本区域会阻止使用 AJAX 将内容加载到其中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51697497/

相关文章:

ajax - Grails 和 AJAX : remoteLink causes redirect, 未更新 div

javascript - "ActionController::UnknownFormat"作为将 AJAX 与 format.js 结合使用的结果

javascript - 将 "this"保留在变量中并将所有 "this"替换为该变量?

javascript - IdentityServer3 - OAuth 流程,不同的方法

跨类和文件的 Javascript 原型(prototype)

javascript - 使用 jQuery 选择评论中包含的元素

javascript - c# 闲置 15 分钟后自动注销

jquery - 无法弄清楚为什么该页面在 Firefox 中看起来很糟糕?在 Chrome/IE 中看起来不错

javascript - 提交图像时删除浏览按钮

php - 使用AJAX将查询发送到php文件时,变量保持为空