javascript - 自动完成在自动完成窗口中显示相关数据

标签 javascript jquery autocomplete jquery-ui-autocomplete autocompletetextview

我有 3 个输入字段,1 个用于数据类型,另外 2 个与其相关。 当我在数据类型字段中按下按钮时,我想显示这样的自动完成窗口 desired

而不是这个

undesired

选择后应该是这样的

result

HTML

<tr>
   <td><input type="text" id="no_1" class="form-control"></td>            
   <td><input type="text" data-type="vehicle" id="vehicle_1" class="type form-control"></td>
   <td><input type="text" id="type_1" class="form-control"></td>
</tr>

JS

$(document).on('focus','.type',function(){
type = $(this).data('type');
if(type =='vehicle' )autoTypeNo = 1;   
$(this).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url : 'autocomplete.php',
            dataType: "json",
            method: 'post',
            data: {
               name_startsWith: request.term,
               type: type
            },
             success: function( data ) {
                 response( $.map( data, function( item ) {
                    var code = item.split("|");
                    return {
                        label: code[autoTypeNo],
                        value: code[autoTypeNo],
                        data : item
                    }
                }));
            }
        });
    },
    autoFocus: true,           
    minLength: 0,
    select: function( event, ui ) {
        var names = ui.item.data.split("|");                       
        id_arr = $(this).attr('id');
        id = id_arr.split("_");
        $('#no_'+id[1]).val(names[0]);
        $('#vehicle_'+id[1]).val(names[1]);
        $('#type_'+id[1]).val(names[2]);
    }              
 });
});

最佳答案

您需要更改 autocomplete.php 然后返回所有 3 个值,您可以轻松地在 json 数组中执行此操作 http://php.net/manual/en/function.json-encode.php然后读取 jquery 脚本中的值。

这是你更新后的 JS 脚本

$(document).on('focus','.type',function(){
type = $(this).data('type');
if(type =='vehicle' )autoTypeNo = 1;   
$(this).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url : 'autocomplete.php',
            dataType: "json",
            method: 'post',
            data: {
               name_startsWith: request.term,
              type: type
            },
             success: function( data ) {
                 response( $.map( data, function( item ) {
                    //var code = item.split("|");
                    return {
                        label: item.no + '-' + item.vehicle + '-' + item.type,
                        value: item.vehicle,
                        data : item
                    }
                }));
            }
        });
    },
    autoFocus: true,           
    minLength: 0,
    select: function( event, ui ) {
        //var names = ui.item.data.split("|");                       
        id_arr = $(this).attr('id');
        id = id_arr.split("_");
        $('#no_'+id[1]).val(ui.item.data.no);
        $('#vehicle_'+id[1]).val(ui.item.data.vehicle);
        $('#type_'+id[1]).val(ui.item.data.type);
    }              
 });
});

关于javascript - 自动完成在自动完成窗口中显示相关数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50881027/

相关文章:

java - 我如何从中间自动填充编辑文本?

javascript - 将 setValue 和 getValue 添加到 JQueryUI 自动完成

javascript - rails如何以一种形式嵌套形式?

javascript - 在两次提交之间强加等待

javascript - AJAX 条件内附加

reactjs - Material-UI - 如何在自动完成中自定义下拉图标?

javascript - 某个类的所有输入到json

javascript - 扩展现有方法

javascript - 迭代 map 标记

javascript - 如何使用jsPDF打印页数相同的 Canvas 上的PDF?