如何将文本字段添加到现有文本字段并将内容传输到附加字段

时间:2014-08-18 05:42:49

标签: jquery

我正在添加/删除我有文本字段的文本字段,当我输入添加另一个文本字段时,当前文本字段的内容应该转移到另一个文本字段,当前文本字段应该为空。 假设我有一个文本字段说Textfield A,其中包含 Hello 作为文本,当我点击“添加其他选项”时,新文本字段应该附加到现有文本字段的顶部A和文本字段A的内容应该转移到B.我怎么能这样做?
我试过以下代码 -

$(function () {


    $('.addScnt').on('click', function (event) {
        event.preventDefault();
        var row = $(this).closest('.row');
        var div_id = parseInt($(row).find('input:text').first().attr('id').split('_id_')[1])||0;
        var div_name= $(row).find('input:text').first().attr('id');
        var div_value= $(row).find('input:text').last().val();


    });

    function createInput(i,vv,name) {

        var p = $('<p />'),
            label = $('<label />', {
                'for': (name) + i
            }),
            input = $('<input />', {
                type: 'text',
                'class': 'cnt',
                size: 20,
                name: (name) +i,
                id: (name) +i,
                value: vv,
                placeholder: 'Input Value'
            }),
            anchor = $('<a />', {
                href: '#',
                id: 'remScnt' + i,
                text: 'Remove',
                on: {
                    click: function () {
                        $(this).closest('p').remove();
                    }
                }
            });

        return p.append(label.append(input), anchor);
    }

    $("form").submit(function(){
        var values = [];
        $.each( $( '#welcome_notes input[id^=Ivrgroupzbase_grpzWelcomeNotes]' ).serializeArray(), function ( _, item ) {
            values.push( item.value );
        } );

        var json = JSON.stringify( { "dataList": values});
        // Do your POSTing here instead of the console.log call
        alert( json );
        $("#Ivrgroupzbase_grpzWelcomeNotes").val(json);

    });


});

FIDDLE

1 个答案:

答案 0 :(得分:0)

您可以使用以下jquery:

$('.addScnt').on('click', function (event) {
            event.preventDefault();
            var row = $(this).closest('.row');
            var input = $(row).find('input:text');
            // read input value
            var value = $(input).val();
            // make input empty
            $(input).val('');
            // get other parent row
            var otherRow = $(row).siblings()[0];
            // find input in other row and put value
            var otherInput = $(otherRow).find('input:text');
            $(otherInput).val($(otherInput).val()+value);
        });

<强> Demo

相关问题