我正在尝试运行聊天应用,但我设置的点击监听器无效。用户单击按钮时没有任何操作。我的代码有问题吗?
这是jquery / javascript脚本
<script>
$(document).on("pageinit",function(event){
setInterval(update, 10000);
setInterval(getMessage, 20000);
$( "#submit" ).bind( "click", function(event, ui) {
//get message
var msg_display = $("#msg").val();
//get chat_id, if not set, display an alert of not able to send, please select chat member
var chat_id = $("#chat_id").val();
//get user_id
var user = $("#user").val();
if(chat_id != ""){
//send message
sendMessage(msg_display, chat_id, user);
}else{
alert("Please select a user to chat with");
$("#msg").val("");
}
});
$( "#m_on li" ).bind( "click", function() {
var selected_member = $(this).html();
window.location = 'chat.php?chat_mate=' + selected_member;
//reload this page with the values of member to chat with
});
function update() {
$.ajax({
type: 'POST',
url: "update.php",
success: function(result){
//all good.
}
})
}
function sendMessage(msg, msg_id, from){
$.ajax({
type: 'POST',
url: "send_message.php",
data: {msg:msg, msg_id:msg_id, from:from},
success: function(result){
if(result == "good"){
getMessage(msg_id);
}else{
alert("Not able to send message " + result);
}
}
})
}
function getMessage(msg_id){
$.ajax({
type: 'POST',
url: "get_message.php",
data: {msg_id:msg_id},
dataType: "json",
success: function(result){
//all good.
///append to the chats.
for(var i=0, i < result.length; i++){
$("#chats").append("<p>" + result[i]['message'] + "</p>" +
"<p align=right>Sent by " + result[i]['sender'] + " at " +
result[i]['time'] + "</p>");
}
$("#msg").val("");
$('#chats').animate({scrollTop:$('#chats').prop("scrollHeight")}, 500);
}
})
}
});
</script>
我在html中的按钮是;
<div id="chats"></div>
<textarea cols="40" rows="8" name="msg" id="msg" placeholder="Message here..."></textarea>
<input type="button" data-inline="true" id="submit" value="Submit">
pageinit
是否与它有关?
答案 0 :(得分:1)
您的for循环语法for(var i=0, i < result.length; i++)
中出现了问题(&#34;,&#34;&#34; var i = 0&#34;应该是&#34 ;;&#34)
也许你应该尝试多调试一下;)