我有两个函数,chatFetch和chatSend
我的问题是 1.我想在点击发送后清除我的文本字段,但似乎无法正确使用 2.当我使用chatFetch时,我想检索并显示已发送的用户名和文本,但现在它只显示" undefined"。
我做错了什么?
<input class="draft" name="input1" id="input1" type="text" value=''/>
<button class="send" name="sendbutton" id="sendbutton" disabled> Send </button>
<ul class="messages">
</ul>
<script>
//delete Chat.display();
//Chat.display("Hello");
var input1 = document.getElementById('input1'), sendbutton = document.getElementById('sendbutton');
function IsEmpty(){
if (input1.value){
sendbutton.removeAttribute('disabled');
} else {
sendbutton.setAttribute('disabled', '');
}
}
input1.onkeyup = IsEmpty;
function chatFetch(){
$.ajax({
url: "https://api.parse.com/1/classes/chats",
dataType: "json",
method: "GET",
success: function(data){
data['results'].sort(function(a, b) {
return a.createdAt < b.createdAt ? -1 : 1;
});
$("ul").empty();
for(var key in data) {
for(var i in data[key]){
//console.log(data[key][i])
$(".messages").append("<li>"+data[key][i].text+"</li>");
}
}
}
})
}
function chatSend(){
$.ajax({
type: "POST",
url: "https://api.parse.com/1/classes/chats",
data: JSON.stringify({text: $('input1.draft').val()}), // stringify the text on value input.draft
success:function(message){
}
})
}
window.setInterval(function(){chatFetch()},3000);
$("#sendbutton").on('click',function(){
chatSend();
chatFetch();
});