简体   繁体   English

如何使用jQuery为Flexdatalist设置默认值

[英]How can I set a default value for Flexdatalist using jQuery

I am using Flexdatalist to provide auto completion functionality. 我正在使用Flexdatalist提供自动完成功能。 My script is something like the following: 我的脚本如下所示:

$('#locationKeyword').flexdatalist({
        url: 'serverAction',
        cache: false,
        searchIn: ["ProvinceName", "CityName", "TownName"],
        visibleProperties: ["ProvinceName", "CityName", "TownName"],
        groupBy: 'ProvinceName',
        value:'1,1,1',
        selectionRequired: false,
        focusFirstResult: true,
        minLength: 1,
        maxShownResults: 20,
        valueProperty: '{Value}',
        textProperty: '{ProvinceName}, {CityName}, {TownName}',
        searchContain: true
    });

The server side code(Asp.NET MVC) returns an array of objects like the following: 服务器端代码(Asp.NET MVC)返回一个对象数组,如下所示:

return Json(new
        {
            ProvinceName = item.Province.Name,
            CityName = item.City.Name,
            TownName = item.Town.Name,
            Value = item.ProvinceId + "," + item.CityId + "," + item.TownId
        });

All item.ProvinceId, item.CityId, item.TownId are int; 所有item.ProvinceId,item.CityId,item.TownId均为int;

The problem: How can I set a default value for it. 问题:如何为它设置默认值。 For example, I want to one combination of Province,City,Town to be selected. 例如,我要选择省,市,镇的一种组合。

Thanks all! 谢谢大家!

I found the solution as below: 我发现解决方案如下:

Html: HTML:

<input name="BusinessLocation" type="text" class="locationKeyword" />

Script: 脚本:

$('[name=BusinessLocation]').flexdatalist({
        url: options.businessLocationUrl,
        cache: false,
        searchIn: ["ProvinceName", "CityName", "TownName"],
        visibleProperties: ["ProvinceName", "CityName", "TownName"],
        groupBy: 'ProvinceName',
        selectionRequired: true,
        focusFirstResult: true,
        minLength: 1,
        maxShownResults: 20,
        textProperty: '{ProvinceName}, {CityName}, {TownName}',
        searchContain: true,
        valueProperty: 'Value'
    });        

    $('.locationKeyword').each(function (index) {
        if ($(this).hasClass('flexdatalist-alias')) {
            $(this).val('تهران, تهران, یوسف آباد');
        } else {
            $(this).val('1,1,1');
        }
    });

    $('[name=BusinessLocation]').val('1,1,1');

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

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