简体   繁体   English

根据下拉列表填充具有自动完成建议的搜索框

[英]Populate search box with autocomplete suggestion based on drop down list

I am trying to create a search box with a drop-down menu. 我正在尝试使用下拉菜单创建搜索框。 So far I have managed to do that. 到目前为止,我已经做到了。 My code is: 我的代码是:

<div id="form_input_things">
<p>
    Search Type:<select id="searchtype" name="searchtype">
            <option value="country">Country</option>
            <option value="city">City</option>
    </select>
    </p>
    <p>
    <div class="form-group">
    Search Term:<input type="text" name="searchterm" id="myMessage" placeholder="Search term" data-toggle="tooltip" data-placement="bottom" title="Search by Country name,City name"  onkeyup='check()'>&nbsp;&nbsp;&nbsp;<input class ="btn btn-primary" type="submit" id="submitid" value="Submit" disabled>
    <script type="text/javascript">
        $("input[type=text]").keyup(function(){
            var count = 0, attr = "disabled", $sub = $("#submitid"), $inputs = $("input[type=text]");  
            $inputs.each(function(){
                count += ($.trim($(this).val())) ? 1:0;
            });
            (count >= $inputs.length ) ? $sub.removeAttr(attr):$sub.attr(attr,attr);
            });
    </script>
    </div>
    </p>
    <p>&nbsp;</p>
</div>

But now the hard part comes, I want to populate search box with a suggested name based on the selection of drop-down menu (list of country name or city name). 但是现在最困难的部分来了,我想根据下拉菜单的选择(国家名称或城市名称列表)为搜索框填充建议的名称。 How can I do that? 我怎样才能做到这一点? I don't want to load the data in my HTML template (because it will make slower to load the page) but want to get all country or city name from my own list (more than 1000s city name). 我不想在HTML模板中加载数据(因为加载页面的速度会变慢),但我想从自己的列表中获取所有国家或城市名称(1000多个城市名称)。 Looking forward to your kind help. 期待您的帮助。

The most difficult part of what you're asking is getting a list of cities and ensuring that you're displaying the cities from that specific country/state/province. 您要查询的最困难的部分是获取城市列表,并确保您显示的是该特定国家/地区/省份的城市。 I personally would suggest using Google Maps' API and adapting it to what you need. 我个人建议使用Google Maps的API,并使其适应您的需求。 See this Link: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete#try-it-yourself 查看此链接: https : //developers.google.com/maps/documentation/javascript/examples/places-autocomplete#try-it-yourself

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM