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

How do I get value of select option for current row

本文关键字:select 选项 何获取 获取      更新时间:2023-09-26

我有动态字段来生成选择选项&文本输入。

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()返回空值?我这里少了什么?

因为当前的select将有一个id,比如itemType_7,但i的值将是8,因为您在创建select后递增它。

因此,在获取您创建的最新select的值时,您应该执行

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