我有一个文本框,它是我的消息文本框。
目前,我的ajax表单是通过按钮的点击工作的。
我猜这是因为按钮的名称。
我希望当我在文本框中按下回车键时,它会提交表单,但不会刷新页面。
添加一些jQuery代码后,按Enter键会使我的页面刷新。
以下是我的刷新页面当前状态的代码。 这个带有按钮的工作状态包含在这个表单中:
我希望当我在文本框中按下回车键时,它会提交表单,但不会刷新页面。
添加一些jQuery代码后,按Enter键会使我的页面刷新。
以下是我的刷新页面当前状态的代码。 这个带有按钮的工作状态包含在这个表单中:
<form class="chat-form form -dark" method="POST" onsubmit="return submitchat();">
<input class="form-control" type="text" name="chat" id="chatbox" placeholder="Type something.." />
<input type='submit' name='send' id='send' class='btn btn-send' value='Send' />
</form>
我的PHP脚本是:
<form class="chat-form form -dark" method="POST" onsubmit="return submitchat();">
<input class="form-control entryMsg" type="text" name="chat" id="chatbox" placeholder="Type something.." />
</form>
我的Javascript / Jquery代码是:
<script>
// Javascript function to submit new chat entered by user
function submitchat(){
if($('#chat').val()=='' || $('#chatbox').val()==' ') return false;
$.ajax({
url:'chat/chat.php',
data:{chat:$('#chatbox').val(),ajaxsend:true},
method:'post',
success:function(data){
$('#result').html(data); // Get the chat records and add it to result div
$('#chatbox').val(''); //Clear chat box after successful submition
document.getElementById('result').scrollTop=document.getElementById('result').scrollHeight; // Bring the scrollbar to bottom of the chat resultbox in case of long chatbox
}
})
return false;
};
// Function to continously check the some has submitted any new chat
setInterval(function(){
$.ajax({
url:'chat/chat.php',
data:{ajaxget:true},
method:'post',
success:function(data){
$('#result').html(data);
}
})
},1000);
// Function to chat history
$(document).ready(function(){
$('#clear').click(function(){
if(!confirm('Are you sure you want to clear chat?'))
return false;
$.ajax({
url:'chat/chat.php',
data:{username:"<?php echo $_SESSION['username'] ?>",ajaxclear:true},
method:'post',
success:function(data){
$('#result').html(data);
}
})
})
})
</script>
<script>
$(document).ready(function() {
$('.entryMsg').keydown(function(event) {
if (event.keyCode == 13) {
this.form.submit();
return false;
}
});
});
</script>`