如何获取当前行的 select 选项的值

How do I get value of select option for current row

我有动态字段可以生成 select 选项和文本输入。

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 ) {}
            ..

为什么 $("#itemType_"+i).val() return 是一个空值?我在这里遗漏了什么?

因为当前的 select 会有一个 iditemType_7i 的值将是 8 因为你在创建后递增它select.

因此,获取您创建的最新 select 的值,您应该这样做

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