此代码用于使用jQuery的聊天应用程序。 fetchMsg
方法运行正常。问题出在入门类textarea中,由于某种原因bind
函数不起作用。它不响应任何关键事件。我尝试了live
函数和on
函数。问题是在javascript中还是我应该将textarea标签更改为输入类型文本标签?
// delcare new js global object
var chat = {}
chat.entry = $('.chat .entry');
// binding keydown event handler
// THIS PART DOESNT WORK
chat.entry.bind('keydown', function(e) {
console.log(e.keyCode);
if (e.keyCode === 13 && e.shiftKey === false) {
chat.throwMsg($(this).val());
}
e.preventDefault();
});
// fetch msgs method
chat.fetchMsg = function() {
$.ajax({
url: 'engine/chat.php',
type: 'post',
data: {
method: 'fetch'
},
success: function(data) {
$('.chat .msgs').html(data);
}
});
}
chat.throwMsg = function(msg) {
if ($.trim(msg).length != 0) {
$.ajax({
url: 'engine/chat.php',
type: 'post',
data: {
method: 'throw',
msg: msg
},
success: function(data) {
chat.fetchMsg();
chat.entry.val('');
}
});
}
}
chat.interval = setInterval(chat.fetchMsg, 2000);
答案 0 :(得分:0)
试试这个
$(document).ready(funciton(){
$(document).on('keydown', '.chat .entry', function (evt) {
console.log(e.keyCode);
if(e.keyCode === 13 && e.shiftKey === false)
{
chat.throwMsg($(this).val());
}
e.preventDefault();
});
});