【问题标题】:JQuery tiered Autocomplete that does not set data for the next tier不为下一层设置数据的 JQuery 分层自动完成
【发布时间】:2011-04-21 00:51:22
【问题描述】:

我正在运行州和省名称的自动完成功能。我想将状态缩写传递给下一个自动完成级联。我创建了一个隐藏输入,并且我想在选择州名称时将其设置为缩写,然后我将选择缩写以用于下一层自动完成以将城市查询限制为仅一个州。在前面的步骤中,我在 .ajax 数据选项中设置了国家单选按钮,因此您可以(通常)看到我想在这个州/城市层中执行的操作。我已经尝试了几件事,做了很多谷歌搜索和阅读 jQuery 书籍,但我需要一些帮助。 stateProvince 输入框自动完成并选择完整的州名。隐藏输入未使用缩写设置。警报为空。我不确定 ajax 成功或 ajax 选择功能。如何让缩写(不是名称)成为隐藏输入的值?然后如何在下一层的 .ajax 数据选项中选择它?这是相关代码。

选定的不带标记符号的 HTML:

输入类型="text" id="stateProvince" name="stateProvince" size="50" maxlength="50" 输入类型="hidden" id="hiddenState" name="hiddenState"

自动完成源 ajax;

    $("#stateProvince").autocomplete
    ({
        source: function( request, response )
        {                           
            //Pass the selected country to the php to limit the stateProvince selection to just that country  
            $.ajax(
            {
                url: "getStateProvince.php",
                data: {
                        term: request.term, 
                        country: $('input[name=country]:checked').val()    //Pass the selected country to php
                      },       
                type: "POST", 
                dataType: "json",
                success: function( data )
                {                                          
                    response( $.map( data, function( item )
                    {
                        return{                                   
                                label: item.stateName,
                                value: item.name,
                                abbrev: item.stateAbbrev                                                                   
                               }
                    }));
                },
                select: function( event,ui )
                {
                $(this).val(ui.item.value);
                $("#hiddenState").$(this).val(ui.item.abbrev);
                }
            });
            alert('|' + $("#hiddenState").val() + '|2nd');  //doesn't trigger here; shows no content when placed in the next tier Autocomplete              
        },           
        minLength: 2
    });

从 php 返回的 JSON 示例:

[{"stateName":"Alabama","name":"Alabama","stateAbbrev":"AL"},{"stateName":"Alaska","name":"Alaska","stateAbbrev" :"AK"}]

【问题讨论】:

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


    【解决方案1】:

    你几乎成功了!我看到以下问题:

    1. select 是 autocomplete 小部件上的一个事件。您现在的代码将select 定义为$.ajax({...}); 调用的一个选项。

    2. 失败的 alert 语句位于自动完成小部件的 source 函数内。我猜它会在您开始输入后立即弹出。它为空的原因是它不在 select 函数内(它发生在您的 AJAX 调用成功返回之前)。

    您可以按如下方式调整您的代码:

    $("#stateProvince").autocomplete
    ({
        source: function( request, response )
        {                           
            //Pass the selected country to the php to limit the stateProvince selection to just that country  
            $.ajax(
            {
                url: "getStateProvince.php",
                data: {
                        term: request.term, 
                        country: $('input[name=country]:checked').val()    //Pass the selected country to php
                      },       
                type: "POST", 
                dataType: "json",
                success: function( data )
                {                                          
                    response( $.map( data, function( item )
                    {
                        return{                                   
                                label: item.stateName,
                                value: item.name,
                                abbrev: item.stateAbbrev                                                                   
                               }
                    }));
                }
            });             
        },      
        select: function( event,ui )
        {
            $(this).val(ui.item.value);
            $("#hiddenState").val(ui.item.abbrev);
            alert('|' + $("#hiddenState").val() + '|2nd');  //doesn't trigger here; shows no content when placed in the next tier Autocomplete 
        },     
        minLength: 2
    });
    

    注意alert 语句和select 事件处理程序的位置。

    【讨论】:

    • 安德鲁,感谢您的帮助。警报现在触发并给我 [object, Object]。我在下一层有另一个警报,它说的也是。但是,州缩写不会传递到下一层。 (如果我“硬编码”一个状态缩写,它可以工作)在选择功能中,我尝试了 ui.item.abbrev、ui.item.stateAbbrev 和 ui.item[2]。我不知道问题是在隐藏输入中设置值还是获取它。我该如何调试?
    • @Mike_Laird:我注意到select 事件处理程序中有一个错误,我会更新我的答案
    • @Mike_Laird:此外,如果您在 Firefox 中使用 Firebug,您可以设置断点并检查 ui.item 的值以查看 abbrev 属性是否存在。
    • @Andrew Whitaker:成功的缩写行上的断点显示正确的状态缩写。选择中警报上的断点显示缩写。通常,选择在框中显示状态名称。所以我相信 hiddenState 输入设置正确。我找不到 ui.item.abbrev 或 Firebug 内容中隐藏输入的值以直接读取。也许你可以建议在哪里看。我目前的问题预感是在 3d 层获取隐藏数据。我有一个 data: 行与上面的行非常相似。 adminCode1:“AK”,创建阿拉斯加响应。 adminCode1: $("#hiddenState").val(),失败
    • @Mike_Laird:您实际上可以直接在控制台中键入一个 jQuery 选择器。所以如果你输入$(#hiddenState").val(),你应该得到州的缩写。如果这会返回您所期望的结果,那么添加对您的问题不起作用的代码可能会有所帮助。
    【解决方案2】:

    这是自动完成的 2d 州级和 3d 城市级的代码。

        //2d tier - stateProvince: autocomplete selection
        //set the country for the 2nd tier to select stateProvince from only the country selected in the 1st tier         
        $("#stateProvince").autocomplete
        ({ 
            source: function( request, response )
            {                          
                //Pass the selected country to the php query manager to limit the stateProvince selection to just that country                                               
                $.ajax(
                { 
                    url: "getStateProvince.php",
                    data: {
                            term: request.term,   
                            country: $('input[name=country]:checked').val()    //Pass the selected country to php
                          },        
                    type: "POST",  // a jQuery ajax POST transmits in querystring format in utf-8
                    dataType: "json",   //return data in json format                                                                                                                                                        
                    success: function( data ) 
                    {                                           
                        response( $.map( data, function( item ) 
                        {
                            return{                                    
                                    label: item.stateName,
                                    value: item.name,
                                    abbrev: item.stateAbbrev                                                                    
                                   }
                        }));
                    }
                });
            },
            select: function( event,ui )
            {
                $(this).val(ui.item.value);
                $("#hiddenState").val($(this).val(ui.item.abbrev));
                //alert('|' + $("#hiddenState").val() + '|1st');  //shows [object,Object] 
            },      
            minLength: 2
        });
    
        //3d tier - city: autocomplete selection  //                        
        $( "#city" ).autocomplete
        ({                 
            source: function( request, response ) 
            {              
                $.ajax(
                {
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: 
                    {
                        featureClass: "P",
                        country: $('input[name=country]:checked').val(),
                        adminCode1: $("#hiddenState").val(), //"AK", //works-delivers only Alaska towns.
                        style: "full",
                        maxRows: 10,
                        name_startsWith: request.term
                    },
    
                    success: function( data ) 
                    {
                        response( $.map( data.geonames, function( item ) 
                        {
                            return{
                                    label: item.name + (item.adminName2 ? ", " + item.adminName2 : "") + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                    value: item.name + (item.adminName2 ? ", " + item.adminName2 : "") + (item.adminName1 ? ", " + item.adminName1 : "")
                                   }
                        }));
                    }
                });
            },
            select: function( event, ui ) 
                {
                    $(this).val(ui.item.value);
                    //ui.item.option.selected = true;
                    //self._trigger( "selected", event, {item: ui.item.option});
                },  //combo box demo uses this to bar illegal input. Other things are needed also. item.option is not defined
            minLength: 2,
    
    
            open: function() 
            {
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
            },
            close: function() 
            {
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 2023-04-05
      相关资源
      最近更新 更多