【问题标题】:jquery autocomplete don't return value on select the optionjquery自动完成在选择选项时不返回值
【发布时间】:2025-12-10 09:25:01
【问题描述】:

我有个问题要问你……

我有一个自动完成一些文本的函数,在选择时我想从函数中返回 2 个值...这是我的代码:

function autoComp(field, srt, file, act) {
      $( field ).autocomplete({
            minLength: srt,
            source: function( request, response ) {
                  $.ajax({
                        url: file,
                        type: 'post',
                        dataType: "json",         
                        data: {'azione':act, 'txt':request.term},
                        success: function(data) {
                              response( $.map( data.result, function( item ) {
                                    return { 
                                          label: item.text, 
                                          value: item.text,
                                          id:  item.id
                                    }
                              }));
                        }
                  });
            },
            select: function( event, ui ) { 
                  var obj = {};
                  obj[0] = ui.item.id;
                  obj[1] = ui.item.label;
                  return obj;
            }
      }); 
}

$(document).on('focus', '.farmCompl', function(){
      obj = autoComp(this, 3, 'search.php', 'music');
      if(obj){
            alert(obj[0]);
            alert(obj[1]);
      }
});

autoComp(...) 想成为一个通用函数,可以在我的项目中的许多地方调用...。 'field' 是要完成的选择器(例如'#text1'),srt 是自动完成的开始 .... Ajax 工作正常,事实上自动完成提供了数据库选项...... 问题在于选项的选择...... 当我选择该选项时,我希望自动完成将 id 和 label 的值返回给调用 autoComp(...) 的函数或事件 我希望已经清楚 再见

【问题讨论】:

    标签: jquery function return-value jquery-autocomplete


    【解决方案1】:

    jQuery Autocomplete

    与其尝试为选择事件设置另一个侦听器,不如使用自动完成功能内置的侦听器。只需使用已在“选择”上调用的函数来做您想做的事情。

    我希望这会有所帮助。

        function autoComp(field, srt, file, act) {
              $( field ).autocomplete({
                    minLength: srt,
                    source: function( request, response ) {
                          $.ajax({
                                url: file,
                                type: 'post',
                                dataType: "json",         
                                data: {'azione':act, 'txt':request.term},
                                success: function(data) {
                                      response( $.map( data.result, function( item ) {
                                            return { 
                                                  label: item.text, 
                                                  value: item.text,
                                                  id:  item.id
                                            }
                                      }));
                                }
                          });
                    },
                    select: function( event, ui ) { 
                        var obj = [];
                        obj[0] = ui.item.id;
                        obj[1] = ui.item.label;
                        alert(obj[0]);
                        alert(obj[1]);
                    }
              }); 
        }
    

    顺便说一句,如果你是creating an array,你应该在声明中使用方括号和[0].. 表示法来获取值。如果要创建对象,请在声明中使用花括号并命名为“。”参数来把事情弄出来。例如,您可以将您的对象声明为var obj = {},但随后将其赋值为obj.id = ui.item.id; 等。有关更多信息,请阅读What is the difference between an array and an object

    【讨论】:

      最近更新 更多