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