【发布时间】:2017-02-04 11:04:39
【问题描述】:
我有点击功能:
$("#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);
});
});
当我尝试添加通过 Enter 键提交输入的功能时,什么也没发生。这个函数带回车提交:
$('#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">
【问题讨论】:
-
在按键上你没有触发你想要的动作,你正在将事件处理程序附加到#but元素
-
谢谢队友,我是 jQuery 新手
-
@Hellbea 你可以检查我编辑的答案,它更优化。
标签: javascript jquery django-templates