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

Roh*_*rma 1 jquery jquery-ui jquery-plugins jquery-selectors

我在我的表单中使用自动完成jquery插件.在此我搜索名称时,自动完成显示名称列表,选择要列出的名称,在输入字段中显示名称.我想在选择名称时选择输入字段中显示名称的ID.请让我知道我该怎么做或任何jquery插件?

谢谢

Ali*_*ghi 5

这是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
    }
});
Run Code Online (Sandbox Code Playgroud)

叫你ajax请求并返回这样的JSON数据:

[{"Value":val1,"Text":"text1"},
{"Value":val2,"Text":"text2"}]
Run Code Online (Sandbox Code Playgroud)

我测试了它.它很棒

祝你好运.Foroughi