javascript - 使用ajax函数后清除textarea

标签 javascript php jquery

我有一个有效的ajax函数,用户在文本区域中输入内容,按回车键,用户输入的任何内容都会输出到屏幕上。现在我正在尝试找到一种在用户按 Enter 后清除文本区域的方法。我该怎么做?

test1.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type = "text/javascript"> 

function ajaxPass(gotoUrl,getValueFrom,output) {

      var input = document.getElementById(getValueFrom).value; // or $('#t').val();

      $.ajax({
           type: "POST",
           url: gotoUrl,
           data: { input : input },
           error: function(xhr,status,error){alert(error);},
           success:function(data) {
           document.getElementById( output ).innerHTML = data;
           }

      });

}

</script>

<textarea id = 'textarea' rows = "25" cols = "50" onKeyDown="if(event.keyCode==13) ajaxPass('robotChat2.php','textarea','output')"> </textarea>
<div id = 'output'> </div>

test2.php

<?php

$input = $_POST['input'];
echo $input;

?>

最佳答案

在函数末尾添加 $("#textarea").val('');

    function ajaxPass(gotoUrl,getValueFrom,output) {

          var input = document.getElementById(getValueFrom).value; // or $('#t').val();

          $.ajax({
               type: "POST",
               url: gotoUrl,
               data: { input : input },
               error: function(xhr,status,error){alert(error);},
               success:function(data) {
               document.getElementById( output ).innerHTML = data;
               }

          });
            $("#"+getValueFrom).val('');
    }

关于javascript - 使用ajax函数后清除textarea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31865206/

相关文章:

javascript - 当用户给出错误答案时,我希望弹出一个图像 - 像巨魔一样

php - 相对于 x,y 位置的散列、多边形形状

php - 如何将 SEOStats 与 Codeigniter 集成?

php - jQuery onClick 事件 "overrides"提交表单

JavaScript 滚动当前 View 的元素 id

PHP:如何使用 smtp 设置发送带附件的电子邮件?

php - 选择 ... 其中 id = 任何值。可能吗?

jQuery 访问 JSON 元素

java - Onchange 通过带有 jQ​​uery 的选择框获取多个值

javascript - 如何使用正则表达式忽略 0 值进行价格验证?