Jquery AutoComplete:如何获取所选项目的ID?
我有一个工作完美的自动完成字段,并从数据库中获取数据。当用户从响应中选择结果时,我想将所选项目的 ID 保存在隐藏字段中。
这是我用于自动完成的代码
$jQNetbmis("input#txt_client_name").autocomplete("autosuggest_clientmaster.php", {
width: 160,
mustMatch: true,
selectFirst:false,
formatResult: function(row) {
var resStr = row.toString();
temp = resStr.substring(0,resStr.indexOf("+"));
return temp;
},
formatItem: function(row, i, max) {
var resStr = row.toString();
var temp = resStr.substring(0,resStr.indexOf("+"));
return temp;
}
});
以下是我按 n 得到的响应
name 1+50
Name 2+85
Name 3+86
Name 4+98
Name 5 +103
如果用户选择名称 1 我想将 50 保存到隐藏字段中。
我正在使用自动完成 - jQuery 插件 1.0.2
Krishnik
I have an auto complete field which is working perfectly, and fetch the data from the database. When user selects a result from the response, i want to save the id of the selected item in a hidden field.
Here is the code I'm using for autocomplete
$jQNetbmis("input#txt_client_name").autocomplete("autosuggest_clientmaster.php", {
width: 160,
mustMatch: true,
selectFirst:false,
formatResult: function(row) {
var resStr = row.toString();
temp = resStr.substring(0,resStr.indexOf("+"));
return temp;
},
formatItem: function(row, i, max) {
var resStr = row.toString();
var temp = resStr.substring(0,resStr.indexOf("+"));
return temp;
}
});
Following is the response that i get i press n
name 1+50
Name 2+85
Name 3+86
Name 4+98
Name 5 +103
If the user selects name 1 i want to save 50 in to the hidden field .
I'm using Autocomplete - jQuery plugin 1.0.2
Krishnik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery UI 有一个出色的自动完成小部件,该小部件有很好的文档记录: http://jqueryui.com/demos/autocomplete/< /a>.您的案例也在那里,因此请查看示例。
jQuery UI has an excellent autocomplete widget which is pretty well documented: http://jqueryui.com/demos/autocomplete/. Your case is also there so go through examples.
我真的不知道你正在使用的自动完成功能,但问题似乎不是来自它。
只需将 id 与隐藏字段的现有值连接起来即可:
I don't really know the autocomplete you're using but the problem doesn't seem to come from it.
Just concatenate the id with the existing value of the hidden field :
您可以使用
result
处理程序来执行此操作。您可能希望如何实现此目的的一个示例是:每次用户选择一个项目时都会运行该处理程序。顺便说一句,您可能想使用 jQuery UI Autocomplete 而不是这个插件,该插件已被弃用。
You can use the
result
handler to do this. An example of how you might wish to accomplish this is:The handler is ran every time the user selects an item. As an aside, you might want to use the jQuery UI Autocomplete instead of this plugin, which is deprecated in favor of that.