Jquery .val() for input only works once
I'm trying to create a chat box for my game.
The user type's their chat into the input:text feild and by ither pressing Enter or clicking the button submits the chat text.
This all works, however for some reason after the first time a user submits a chat message it fails to get the text from the input field.
Here is my code.
$(document).ready(function() {
$("#chatEnter").live('click',function(){
var chat = $('#chatText').val();
sendChat(chat);
});
});
$(document).ready(function() {
$("#ch开发者_StackOverflow社区atText").keypress(function(e){
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13))
{
var chat = $('#chatText').val();
sendChat(chat);
return false;
}
else return true;
});
});
function sendChat(chat)
{
alert(chat); //temp test alert
$.getJSON("includes/boardUpdate.php",{chat: chat, bid: bid});
$('#chatText').val("");
}
It doesn't matter if i first submit a text by clicking the button or pressing enter, all future attempts submit blank entrys until I refresh the page.
Edit: I've tried it with and without the line to clear the text box, same results both ways.
Your help is appreciated.
It offends The Spirits to have more than one element on the page with the same id. Bad, weird, sometimes supernatural things happen. Often a page might seem to work for a long time, but then fail as soon as it's tweaked.
(Also thanks everybody for prodding me to provide the answer!!)
精彩评论