Jquery Autocomplete选择事件获取json对象

时间:2014-01-08 03:02:39

标签: jquery json jquery-ui jquery-autocomplete jquery-ui-autocomplete

您好我有一个实现jquery自动完成的文本字段。一切都很好。

我现在需要的是“选择”事件我希望能够读取项目json对象。现在,只有两个可用的属性是“label”和“value”。

以下是代码:

$( "#txtfatherslast" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: '@Url.Content("~/Home/SearchVisitor")/',
                    type: 'POST',
                    contentType: 'application/json',
                    dataType: "json",
                    data: JSON.stringify(
                     {
                         fathersLast: "",
                         mothersLast: "",
                         firstName: ""

                     }),
                    success: function( data ) {
                        response( $.map( data.results, function( item ) {
                            return {
                                label: item.FathersLast + " " + item.MothersLast + ", " + item.FirstName + " (" + item.OrganizationName + ")", 
                                value: item.FathersLast
                            }
                        }));
                    }
                });
            },
            minLength: 3,
            select: function( event, ui ) {
                alert(ui.item.label); // here I would like to get item.FathersLast etc...
            },
            open: function() {
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
            },
            close: function() {
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
            }
        });

所以Select事件:

 ....
    minLength: 3,
                select: function( event, ui ) {

// here i want to be able to read item.FathersLast or item.MothersLast.. is that possible?                    

alert(ui.item.label);

                },
    ...

关于如何在Selected事件中获取json对象属性的任何线索?

由于

1 个答案:

答案 0 :(得分:0)

当您填充自动填充时,您只需使用来自JSON obj的属性值添加您希望在来自响应的对象中检索的属性,例如:

source: function( request, response ) {
            $.ajax({
                url: '@Url.Content("~/Home/SearchVisitor")/',
                type: 'POST',
                contentType: 'application/json',
                dataType: "json",
                data: JSON.stringify(
                 {
                     fathersLast: "",
                     mothersLast: "",
                     firstName: ""

                 }),
                success: function( data ) {
                    response( $.map( data.results, function( item ) {
                        return {
                            label: item.FathersLast + " " + item.MothersLast + ", " + item.FirstName + " (" + item.OrganizationName + ")", 
                            value: item.FathersLast,
                            /* edits */
                            fathersLast : item.FathersLast, //access as ui.item.fathersLast
                            mothersLast: item.MothersLast, //access as ui.item.mothersLast, etc [...]
                            firstName : item.FirstName,
                            organizationName : item.OrganizationName
                            //[...]
                        }
                    }));
                }
            });
        }