jQuery自动完成选择名称和显示ID

编程入门 行业动态 更新时间:2024-10-26 03:35:31
本文介绍了jQuery自动完成选择名称和显示ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在表单中使用自动完成的jquery插件.在这种情况下,当我搜索名称时,自动完成功能将在选择名称时显示名称列表,以列出名称,从而在输入字段中显示名称.当我选择名称时,我希望onselection在输入字段中显示名称的ID.请让我知道我该如何做或使用任何jquery插件?

谢谢

解决方案

这是Jquery代码:

$("#txt1").autocomplete({ source: function (request, response){ $.ajax({ type: "POST", url: "YourAddress", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { response($.map(data.d, function (item) { return { id: item.Value, value: item.Text } })) } }); }, select: function (event, ui) { $("#hdnId").val(ui.item.id);//Put Id in a hidden field } });

调用ajax请求并返回JSON数据,如下所示:

[{"Value":val1,"Text":"text1"}, {"Value":val2,"Text":"text2"}]

我测试了它.很棒的人

祝你好运.

I am using the autocomplete jquery plugin in my form. In this when I search the name , autocomplete display the list of the names onselection the names to list it display the name in the input field. I want onselection when I select the name it display the id of the name in input field.Please let me know How can I do it or any jquery plugin ?

Thanks

解决方案

this is Jquery Code :

$("#txt1").autocomplete({ source: function (request, response){ $.ajax({ type: "POST", url: "YourAddress", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { response($.map(data.d, function (item) { return { id: item.Value, value: item.Text } })) } }); }, select: function (event, ui) { $("#hdnId").val(ui.item.id);//Put Id in a hidden field } });

call you ajax request and return JSON data something like this :

[{"Value":val1,"Text":"text1"}, {"Value":val2,"Text":"text2"}]

I tested it.It works great man

Good luck.Foroughi

更多推荐

jQuery自动完成选择名称和显示ID

本文发布于:2023-10-30 18:56:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543712.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自动完成   名称   jQuery   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!