【问题标题】:JQuery autocomplete() works only in first row of tableJQuery autocomplete() 仅适用于表的第一行
【发布时间】:2021-09-07 19:00:52
【问题描述】:

自动完成 jQuery UI 仅适用于表格的第一行。 我正在使用 Bootstrap 上的 Autocomplete jQuery UI 开发药房搜索引擎表单,问题是自动完成功能仅适用于第一行。当我单击添加按钮时,它会创建一个新行,但在新行中它不起作用。

我的 jQuery 语法如下:

$("#itemSearch").autocomplete({
    source: "{{ route('autoCompSearch') }}",
    minLength:3,
    select: function(key, value){
        console.log(key, value);
    },
});

我的 addRow() 如下;

$('#journalRows').on('keydown', '#addRow',function(event){
    // alert('you are here...');
    count++;
    dynamic_field(count);
    return true;
    }

而我的 dynamic_filed(count) 函数是 as;

function dynamic_field(number)
{
    // New Function to add New Row in Journal
    var html = '';

    html += '<tr class="options" name="line_items">'
    html += '<td width="5%"><input type="text" id="checkbox. $user->id ." name="id[]" class="form-control" value="" disabled=""></td>'
    html += '<td width="15%"><input type="text" name="barcode_id[]" id="barcodeSearch" class="form-control"></td>'
    html += '<td width="30%"><input type="text" name="item_id[]" id="itemSearch" class="form-control select-product"></td>'
    html += '<td width="5%"><input type="text" name="qty[]" value="1" class="form-control calc" onkeyup="InvoiceCalculation()"></td>'
    html += '<td width="10%"><input type="text" id="sal_price" value="1" name="sal_price[]" class="form-control calc" onkeyup="InvoiceCalculation()"></td>'
    html += '<td width="5%"><input type="text" name="discPer[]" class="form-control" value="0" placeholder="Discount percentage..." onkeyup="InvoiceCalculation()"></td>'
    html += '<td width="15%"><input type="text" id="totUnitAmount" name="totUnitAmount[]" class="form-control form-control-sm" value="0"></td>'
    html += '<td><input type="button" id="addRow" name="addRow[]" class="form-control btn btn-success" value="+"></td>'
    html += '<td><input type="button" id="removeRow" name="removeRow[]" class="form-control btn btn-danger" value="-"></td>';
    html += '</tr>';

    $('#journalRows').append(html);
};

这是可行的,但只有自动完成选项在第二行及以后的行中不起作用.....

【问题讨论】:

    标签: javascript autocomplete


    【解决方案1】:

    对于新行,您需要更改自动完成字段的 ID。每个自动完成字段都应该有一个唯一的 id:

    $("#itemSearch").autocomplete({
        source: "{{ route('autoCompSearch') }}",
        minLength:3,
        select: function(key, value){
            console.log(key, value);
        },
    });
    
    $("#itemSearch1").autocomplete({
        source: "{{ route('autoCompSearch') }}",
        minLength:3,
        select: function(key, value){
            console.log(key, value);
        },
    });
    

    还需要在字段创建后初始化自动完成模块,否则它不会在 DOM 中,jQuery 将无法用于字段

    更新:

    如下更改您的 dynamic_field 函数:

    function dynamic_field(number)
    {
        // New Function to add New Row in Journal
        var html = '';
    
        html += '<tr class="options" name="line_items">'
        html += '<td width="5%"><input type="text" id="checkbox. $user->id ." name="id[]" class="form-control" value="" disabled=""></td>'
        html += '<td width="15%"><input type="text" name="barcode_id[]" id="barcodeSearch" class="form-control"></td>'
        html += '<td width="30%"><input type="text" name="item_id[]" id="itemSearch-'+number+'" class="form-control select-product"></td>'
        html += '<td width="5%"><input type="text" name="qty[]" value="1" class="form-control calc" onkeyup="InvoiceCalculation()"></td>'
        html += '<td width="10%"><input type="text" id="sal_price" value="1" name="sal_price[]" class="form-control calc" onkeyup="InvoiceCalculation()"></td>'
        html += '<td width="5%"><input type="text" name="discPer[]" class="form-control" value="0" placeholder="Discount percentage..." onkeyup="InvoiceCalculation()"></td>'
        html += '<td width="15%"><input type="text" id="totUnitAmount" name="totUnitAmount[]" class="form-control form-control-sm" value="0"></td>'
        html += '<td><input type="button" id="addRow" name="addRow[]" class="form-control btn btn-success" value="+"></td>'
        html += '<td><input type="button" id="removeRow" name="removeRow[]" class="form-control btn btn-danger" value="-"></td>';
        html += '</tr>';
    
        $('#journalRows').append(html);
        $("#itemSearch-"+number).autocomplete({
              source: "{{ route('autoCompSearch') }}",
              minLength:3,
              select: function(key, value){
                 console.log(key, value);
              },
        });
    };
    

    【讨论】:

    • Thanx Harshith,但我如何在创建后在字段上初始化自动完成模块。请在这方面指导我。实际上,我是 JavaScript 新手。
    • 当您点击添加新行按钮时。将此代码放在该函数中
    • 我也添加了我的 addrow()。我按照您的建议尝试了,但我无法成功。
    • 更新了我的答案,请检查
    • 好吧,唐。严打其工作。非常感谢。你太棒了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 2016-03-27
    • 2021-02-12
    • 2016-12-12
    相关资源
    最近更新 更多