首页 > 解决方案 > 使用 typeahead.js 使用多个数据集时更改输入字段的名称属性

问题描述

我正在使用带有多个数据集的 typeahead.js,如此所述。我没有使用 Bloodhound,所以我的代码看起来有点不同,如下所示

$('#search .typeahead').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'football_team',
  source: substringMatcher(football_teams),
  templates: {
    header: '<h3>Football teams</h3>'
  }
},
,{
  name: 'basketball_team',
  source: substringMatcher(basketball_teams),
  templates: {
    header: '<h3>Basketball teams</h3>'
  }
});

由于它正在使用数据集football_teamsbasketball_teams我想根据使用的数据集发送不同的 GET 请求。例如,如果我搜索“Arsenal”,选择它并单击 Enter,它应该会发送以下请求?football_team=Arsenal

如何name根据使用的数据集设置输入字段的属性?

我认为它应该很容易实现,但是在谷歌搜索了一段时间后我找不到任何东西......

标签: jqueryhtmlformstypeahead.js

解决方案


使用cdn-ref try with custom-eventhandler for typeahead:select处理选定的选项

用法参考:示例代码(内部项目)


推荐阅读