输入时将文本框值附加到textarea [英] append textbox value to textarea on enter

查看:124
本文介绍了输入时将文本框值附加到textarea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当按下 enter 按钮时,我希望将textbox值附加到textarea的新行上.我已经可以使用 enter 了,但是我不确定如何附加textbox值.我尝试了一些东西,但是没用.

When the enter button is pressed, I'd like the textbox value to be appended on a new line to a textarea. I've already got the enter working but I'm not sure how to append the textbox value. I tried something but it didn't work.

$(function() {
    $("#quickLinksTextbox input").keypress(function (e) {
        if (e.keyCode == 13) {
            $("#quickLinksURLs").val().append("#quickLinksSave");
        }
    });
});

推荐答案

检查我的代码.

JSFiddle

$(document).on('keypress', '#quickLinksTextbox input', function (e){

    var inputEl = $(this);
    var textareaEl = $('#quickLinksURLs');

    //Enter was pressed
    if(e.keyCode == 13){

        //If input have any text
        if($(inputEl).val().length){

            //Appending current content from input (with new line ending) to textarea
            $(textareaEl).val($(textareaEl).val()+$(inputEl).val()+"\r\n");

            //Cleaning input
            $(inputEl).val('');
        }

        return false;
    }
});

这篇关于输入时将文本框值附加到textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆