Php 世界城市排行榜

Php 世界城市排行榜,php,jquery,sql-server,asp.net-mvc,internationalization,Php,Jquery,Sql Server,Asp.net Mvc,Internationalization,是否有任何解决方案向用户显示所有国家/地区,并在选择国家/地区后重新选择其国家/地区的所有城市?最好选择脚本 p、 如果您只需要一个一次性列表(而不是不断更新的列表),那么手动删除国家名称也不错。也可以用城市列表做类似的事情,但试图得到一个完整的城市列表有点鲁莽。我会尽量把它限制在世界前200名左右 注意:我只是假设这是国家列表页面,因为我不会说俄语,但这是我搜索该页面时得到的第一个结果。当然,有一个解决方案。您可以拥有具有Id和Name属性的Country和City型号: public cla

是否有任何解决方案向用户显示所有国家/地区,并在选择国家/地区后重新选择其国家/地区的所有城市?最好选择脚本


p、 如果您只需要一个一次性列表(而不是不断更新的列表),那么手动删除国家名称也不错。也可以用城市列表做类似的事情,但试图得到一个完整的城市列表有点鲁莽。我会尽量把它限制在世界前200名左右


注意:我只是假设这是国家列表页面,因为我不会说俄语,但这是我搜索该页面时得到的第一个结果。

当然,有一个解决方案。您可以拥有具有
Id
Name
属性的
Country
City
型号:

public class Country
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
}
这项行动将给你一个国家的所有城市:

public class CountriesController : Controller
{
    public ActionResult Index()
    {
        IEnumerable<Country> countries = Repository.GetCountries();
        return View(countries);
    }
}

public class CitiesController: Controller
{
    public ActionResult Index(string countryId)
    {
        IEnumerable<City> cities = Repository.GetCities(countryId);
        return Json(cities);
    }
}
公共类countries控制器:控制器
{
公共行动结果索引()
{
IEnumerable countries=Repository.GetCountries();
返回视图(国家);
}
}
公共类CitiesController:控制器
{
公共操作结果索引(字符串countryId)
{
IEnumerable cities=Repository.GetCities(countryId);
返回(城市);
}
}
并有类似的看法:

<%= Html.DropDownList("selectedCountry", new SelectList(Model, "Id", "Name")) %>
<%= Html.DropDownList("selectedCity", Enumerable.Empty<City>()) %>

然后安装javascript:

$(function() {
    $('#selectedCountry').change(function() {
        var selectedCountry = $(this).val();
        $.getJSON('/cities/index', { countryId: selectedCountry }, function(cities) {
            var citiesSelect = $('#selectedCity');
            citiesSelect.empty();
            $(json).each(function(i, city) {
                citiesSelect.append('<option value="' + city.Id + '">' + city.Name + '</option>');
            });
        });
    });
});
$(函数(){
$('#selectedCountry')。更改(函数(){
var selectedCountry=$(this.val();
$.getJSON('/cities/index',{countryId:selectedCountry},函数(cities){
var citiesSelect=$(“#selectedCity”);
citiesSelect.empty();
$(json).每个(函数(i,城市){
CitieSelect.append(“”+city.Name+“”);
});
});
});
});

我认为,更好的解决方案是使用JSON来创建数据库。例如,您可以使用geonames.org

例如:

<meta charset="utf-8">  
    <style>
    .ui-autocomplete-loading { background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat; }
    #city { width: 25em; }
    </style>
    <script>
    $(function() {
        function log( message ) {
            $( "<div/>" ).text( message ).prependTo( "#log" );
            $( "#log" ).attr( "scrollTop", 0 );
        }

        $( "#city" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
                        featureClass: "P",
                        style: "full",
                        maxRows: 12,
                        name_startsWith: request.term
                    },
                    success: function( data ) {
                        response( $.map( data.geonames, function( item ) {
                            return {
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                value: item.name
                            }
                        }));
                    }
                });
            },
            minLength: 2,
            select: function( event, ui ) {
                log( ui.item ?
                    "Selected: " + ui.item.label :
                    "Nothing selected, input was " + this.value);
            },
            open: function() {
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
            },
            close: function() {
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
            }
        });
    });
    </script>



<div class="demo">

<div class="ui-widget">
    <label for="city">Your city: </label>
    <input id="city" />
    Powered by <a href="http://geonames.org">geonames.org</a>
</div>

<div class="ui-widget" style="margin-top:2em; font-family:Arial">
    Result:
    <div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
</div>

</div><!-- End demo -->



<div class="demo-description">
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are cities, displayed when at least two characters are entered into the field.</p>
<p>In this case, the datasource is the <a href="http://geonames.org">geonames.org webservice</a>. While only the city name itself ends up in the input after selecting an element, more info is displayed in the suggestions to help find the right entry. That data is also available in callbacks, as illustrated by the Result area below the input.</p>
</div><!-- End demo-description -->

.ui自动完成加载{背景:白色url('images/ui-anim_basic_16x16.gif')右中心无重复;}
#城市{宽度:25em;}
$(函数(){
功能日志(消息){
$(“”).text(message).prependTo(“#log”);
$(“#log”).attr(“scrollTop”,0);
}
$(“#城市”).autocomplete({
来源:功能(请求、响应){
$.ajax({
url:“http://ws.geonames.org/searchJSON",
数据类型:“jsonp”,
数据:{
特色类:“P”,
样式:“完整”,
马克斯罗:12,
name_startsWith:request.term
},
成功:功能(数据){
响应($.map(data.geonames,函数(项)){
返回{
标签:item.name+(item.adminName1?,“+item.adminName1:”)+,“+item.countryName,
值:item.name
}
}));
}
});
},
最小长度:2,
选择:功能(事件、用户界面){
日志(ui.item?
“选定:”+ui.item.label:
“未选择任何内容,输入为”+此值);
},
打开:函数(){
$(this).removeClass(“ui角点全部”).addClass(“ui角点顶部”);
},
关闭:函数(){
$(this.removeClass(“ui角顶部”).addClass(“ui角全部”);
}
});
});
您所在的城市:
技术支持
结果:
“自动完成”窗口小部件在您键入字段时提供建议。这里的建议是城市,在字段中输入至少两个字符时显示

在本例中,数据源是。虽然在选择元素后,只有城市名称本身会出现在输入中,但建议中会显示更多信息,以帮助找到正确的条目。该数据也可以在回调中使用,如输入下面的结果区域所示


您是否已有国家及其城市的数据库?你说的“最好的剧本选择”是什么意思?不,我试着找到它。最佳变体-国家表、州表、城市表。使用脚本,我的意思是-1下拉选择county,然后使用ajax重新加载states,thes重新加载citiesthanks。但最好是提供一个包含国家和城市的数据库;)