0
功能我有点击功能:点击通过Enrr
$("#but").on('click', function(){
$.post('/chat/send/', {
id: $(this).attr("name"), text: $('#text').val(), chat_id: $(".message-box").attr("chat-id"),
}, function(response) {
var options = '';
$.each(response.data, function() {
options += '<p><b>' + this[0] + ':</b> ' + this[1] + '</p>';
});
$('#text').val('');
$('.message-box').html(options);
});
});
当我尝试通过添加回车键也没有什么happend submiting输入的功能。这与输入功能提交:
$('#text').keypress(function(e){
if(e.which == 13){
$("#but").on('click', function(){
$.post('/chat/send/', {
id: $(this).attr("name"), text: $('#text').val(), chat_id: $(".message-box").attr("chat-id"),
}, function(response) {
var options = '';
$.each(response.data, function() {
options += '<p><b>' + this[0] + ':</b> ' + this[1] + '</p>';
});
$('#text').val('');
$('.message-box').html(options);
});
});
}
});
在HTML我有:
<input id="text" type="text" name="message-text" size="63">
<input id="but" type="submit" name="Send" value="Send">
on keypress你不是没有发射你想要的动作,你正在附加事件处理程序到#but元素 – Nic
Thx队友,我是新的jQuery – Hellbea
@Hellbea你可以检查我编辑的答案,它是更优化的。 – PacMan