简体   繁体   English

jQuery textarea.val('')在FIRST Enter上添加换行符吗?

[英]Jquery textarea.val('') adds line break on FIRST enter press?

I have a little chat setup, and on enter press if the text area is in focus I have it set to submit the chat to my database and clear the text area. 我有一个小的聊天设置,如果文本区域处于焦点位置,请按Enter并设置为将聊天提交到我的数据库并清除文本区域。 Unfortunately though, the first time one presses enter it adds a linebreak in the text area, in any browser. 但是不幸的是,在任何浏览器中,第一次按Enter都会在文本区域添加换行符。 If you type and press enter again, there's still only one line break there. 如果您键入并再次按Enter,则那里只有一个换行符。 Am I missing something? 我想念什么吗?

Thanks! 谢谢!

    $(document).keypress(function(keyPress) {
        if (keyPress.which == 13) {
            if ($('#chatText').is(':focus')) {
                if ($('#chatText').val().length > 0) {
                    chatValue = $('#chatText').val();
                    $('#chatText').val($('#chatText').val().substring(0,0));
                    $.ajax({
                        type: 'POST',
                        url: 'submitChat.php',
                        data: { chatText: chatValue },
                        success: function(result) {
                            $('#chat_text').html(result);
                            document.getElementById('chat_text').scrollTop = 9999999;
                        }
                    });
                }
            }
        }
    });

Why don't you just clear it? 你为什么不清除它?

$('#chatText').keypress(function(e) {
    if (e.which == 13) {
        var value = $(this).val();

        if (value.length > 0) {
            $(this).val('');

            $.ajax({
                type: 'POST',
                url: 'submitChat.php',
                data: {
                    chatText: value
                },
                success: function(result) {
                    $('#chat_text').html(result);
                    this.scrollTop = 9999999;
                }
            });
        }
    }
});

Try this, 尝试这个,

$(document).keypress(function(keyPress) {
            if (keyPress.which == 13) {
                keyPress.preventDefault();
                if ($('#chatText').is(':focus')) {
                    if ($('#chatText').val().length > 0) {
                        chatValue = $('#chatText').val();
                        $('#chatText').empty();
                        $.ajax({
                            type: 'POST',
                            url: 'submitChat.php',
                            data: { chatText: chatValue },
                            success: function(result) {
                                $('#chat_text').html(result);
                                document.getElementById('chat_text').scrollTop = 9999999;
                            }
                        });
                    }
                }
            }
        });
 $(document).keypress(function(keyPress) {


        if (keyPress.which == 13) {
            keyPress.preventDefault();// Add this line
            if ($('#chatText').is(':focus')) {
                if ($('#chatText').val().length > 0) {
                    chatValue = $('#chatText').val();
                    $('#chatText').val($('#chatText').val().substring(0,0));
                    $.ajax({
                        type: 'POST',
                        url: 'submitChat.php',
                        data: { chatText: chatValue },
                        success: function(result) {
                            $('#chat_text').html(result);
                            document.getElementById('chat_text').scrollTop = 9999999;
                        }
                    });
                }
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM