获取动态创建的文本框的值

时间:2018-04-19 11:39:50

标签: javascript dynamic textbox

我想获取动态创建的文本框值。 我的js代码是,



$('#Dynm_Device_Fields').append('<div class="col-sm-12 form-group"><div class="col-sm-1"><button type="button" name="add" class="btn btn-success btn-sm add"><span class="glyphicon glyphicon-plus"></span></button></div></div>');

$(document).on('click','.add',function(){
        inner_div = "";
        id_cnt = id_cnt + 1;
        inner_div = "<div class='col-sm-12 form-group'><label class='col-sm-4 control-label'></label><div class='col-sm-7'><div class='col-sm-4'><input name = 'Dynm_Key_"+id_cnt+"' id='Dynm_Key_"+id_cnt+"' type='text'  class='form-control adcol' placeholder='Field Name' /></div><div class='col-sm-4'><input name = 'Dynm_Val_"+id_cnt+"' id='Dynm_Val_"+id_cnt+"' type='text' class='form-control adcol' placeholder='Field Value' /></div></div><button name = 'DynmButtonBox' type='button' style='width:35px;' class='btn btn-danger remove col-sm-1 fa fa-close' data-toggle='tooltip' data-placement='right' data-original-title='Remove' ></button><br></div>";
        $('#Dynm_Device_Fields').append(inner_div);
});
&#13;
&#13;
&#13;

请帮助找出解决方案。 感谢

2 个答案:

答案 0 :(得分:1)

我看到的文本框是:

  ArrayList<String> mCombinedList; // it contains some value I have already instantiate it  
    StringBuilder builder = new StringBuilder();
    for (String singleLine : mCombinedList) {
        if (builder.length() > 0) {
            builder.append(" ");
        }
        builder.append(singleLine);

        }
    String string = builder.toString();
    Log.e(TAG, "string builder "  +string );

使用JQuery:

<input name='Dynm_Val_"+id_cnt+"' 
    id='Dynm_Val_"+id_cnt+"' 
    type='text' 
    class='form-control adcol' 
    placeholder='Field Value' />

如果您需要获取所有文本框的数组:

var val = $('#Dynm_Val_' + id_cnt).val();

或纯JavaScript,请尝试:

var textboxElements = $('input.adcol');

答案 1 :(得分:0)

因为看起来你有.addcol动态添加的文本框。试试这个

$(document).on('click', '.adcol', function() { alert($(this).val()) });