how i can get the value of input text when enter key pressed in this cod ? .....................................................................
<body>
<div id="center">
<div id="boxtop"></div>
<div id="boxdown">
<input type="text" id="txt"/>
<button type="submit" onclick="SaveData()" id="btn" >SEND</button>
</div>
</div>
<script>
function SaveData() {
work = "insert";
chat = $('#txt').val();
$.ajax({
type: "POST",
url: "server.php",
data: "work="+work+"&chat="+chat,
success: function(msg){
$('#txt').val('');
}
});
}
</script>
</body>
You should add a form around your button and input. Then you can handle both events with
$('form').on('submit', function() {
SaveData();
return false;
});
If you want to fire an event with ajax function you do not need submit button.
$('form').on('submit', function(){
preventDefault(); ....
Why you submit the form and then prevent submit?
It is better to do this:
$(document).keydown(function (e)
{
if(e.keyCode == 13)
SaveData();
});
$('#txt').keypress(function (e) {
if(e.which == 13) {
chat = $('#txt').val();
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.