jQuery 自动完成选择填充其他字段
我调用远程数据源并返回 json。选择一个项目后,select:
回调选项仅允许我使用该项目的标签和值,但我还想使用 json 对象的其他属性来自动填充其他字段。
有没有一种我缺少的便捷方法可以做到这一点?到目前为止我看到的选项是...
- 全局缓存ajax响应json对象,并在选择后引用此全局对象
- 使用项目的值或标签在选择时重新查询数据库
对其中任何一个都不是特别满意。想法?
编辑 我忘记我正在使用 $.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");
});
I call a remote data source and get back json. After I select an item the select:
callback option only allows me to work with the item's label and value, but I would also like to work with the other properties of my json object to autopopulate other fields.
Is there a convenient way to do this that I am missing? The options I see so far are...
- Cache the ajax response json object globally and reference this global object after select
- Re-query the database on select using the item's value or label
Not particularly satisfied about either of those. Thoughts?
Edit
I forgot I was using $.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");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是什么让您认为您只能使用标签和价值?
what makes you think you can only work with the label and value?