简体   繁体   中英

How do I get value of select option for current row

I have dynamic field to generate select option & text input.

var i=$('table tr').length;
$(".addmore").on('click',function(){
    html = '<tr>';
    html += '<td><select name="itemType[]" id="itemType_'+i+'" class="form-control input-sm"><option value="AAA">AAA</option><option value="BBB">BBB</option></select></td>';
    .....
    .....
    html += '</tr>';
    $('table').append(html);
    i++;
});
$(document).on('focus','.autocomplete_txt',function(){
    type = $(this).data('type');
    if(type =='itemName' )autoTypeNo=0;
    $(this).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url : 'ajax',
                dataType: "json",
                method: 'post',
                data: {
                    search : request.term,
                    type : type,
                    select : $("#itemType_"+i).val()
                }
                success: function( data ) {}
            ..

Why the $("#itemType_"+i).val() return an empty value? I have missing something here?

Because the current select will have a id say itemType_7 but the value of i will be 8 as you are incrementing it after creating the select .

So the fetch the value of latest select you have created, you should do

$("#itemType_"+(i-1)).val()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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