jQuery在选择填充其他字段时自动完成

jQuery autocomplete on select fill in other fields

本文关键字:字段 选择 填充 其他 jQuery      更新时间:2023-09-26

我调用一个远程数据源并返回json。在我选择一个项目后,select:回调选项只允许我使用项目的标签和值,但我也想使用json对象的其他属性来自动填充其他字段。

是否有一种方便的方法来做到这一点,我错过了?到目前为止,我看到的选项是…

  1. 全局缓存ajax响应json对象,并在选择
  2. 后引用该全局对象
  3. 使用物品的值或标签重新查询数据库中的select

对这两个都不是特别满意。想法吗?

编辑我忘了我用了$.map

$('#accountName').autocomplete({
  source: function (request, response) {
      $.getAccountsByNameLike(request.term, function (data) {
          response($.map(data, function (item) {
              return {
                  label: item.Name + ' (' + item.Address.City + ', ' + item.Address.StateOrProvince + ')',
                  value: item.AccountId,
                  // Added to fix issue
                  raw: item
              }
          }));
      }, function (error) {
          // async kickoff a log to logging server service...
          alert("There was a problem while trying to retrieve account names. Please contact support");
      });

是什么让你认为你只能使用标签和值?

select: function (event, ui) {
       foo = ui.item.label;
        $("#bar").val(ui.item.id);
       baz = (ui.item.JsonField);
    }