Jquery autocomplete select name and display id [cl

2019-07-29 05:21发布

问题:

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

回答1:

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