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

时间:2017-04-03 14:25:45

标签: javascript jquery

我正在使用Flexdatalist来提供自动完成功能。 我的脚本如下所示:

$('#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
    });

服务器端代码(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
        });

所有item.ProvinceId,item.CityId,item.TownId都是int;

问题: 如何为其设置默认值。例如,我想要选择省,市,镇的一个组合。

1 个答案:

答案 0 :(得分:0)

全部谢谢!

我找到了解决方案如下:

HTML:

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

脚本:

$('[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');