jQuery自动完成选择填写其他字段

时间:2011-09-09 18:29:17

标签: javascript jquery json jquery-ui autocomplete

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

有没有一种方便的方法可以做到这一点,我错过了?我到目前为止看到的选项是......

  1. 全局缓存ajax响应json对象,并在选择
  2. 后引用此全局对象
  3. 使用项目的值或标签
  4. 在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");
          });
    

1 个答案:

答案 0 :(得分:4)

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

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