jQuery, new line and enter button
I'm using following code for textarea:
$('#ajaxSendMessage').live('keyup', function(event) {
if (event.keyCode == 13) {
controller.sendMessage($(this).val(), $("#ajaxAnswerTo").val());
}
});
This code works, but开发者_如何学Python $("#ajaxAnswerTo").val()
have new line characte when I click enter...
For example: I entered: helo world and then moved cursor to helo world, updated it to hello and clicked enter. The result code will be: hell\no world.
How to remove this \n?
I think you want to catch it on keydown
see it working here on jsfiddle
If you want to go the regex route, you can simply use
$("#ajaxAnswerTo").val().replace(/\r?\n/, '');
A simple Regular Expression should do the trick:
$("#ajaxAnswerTo").val().replace(/\r?\n/, '');
$('#ajaxSendMessage').live('keypress', function(event) {
if (event.keyCode == 13) {
event.preventDefault();
controller.sendMessage($(this).val(), $("#ajaxAnswerTo").val());
}
});
精彩评论