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