开发者

jQuery, new line and enter button

开发者 https://www.devze.com 2023-03-28 17:57 出处:网络
I\'m using following code for textarea: $(\'#ajaxSendMessage\').live(\'keyup\', function(event) { if (event.keyCode == 13) {

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());
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消