1. 程式人生 > >Ajax初始化下拉框資料

Ajax初始化下拉框資料

html
 <input class="easyui-combobox" id="ddl_yearTime" style="width: 100px;" editable="false"
                                        data-options="
                                        method: 'get',
                                        panelHeight:'auto'
                                    " />

js

function  LoadCombox(){
    //載入年份
    $('#ddl_yearTime').combobox({
        url:ActionURL + "loadComYear",
        valueField: 'id',
        textField: 'text',
        onSelect: function (ret) {
            yyear = ret.id;   // yyear是全域性變數
        },
        onLoadSuccess: function () { //載入完成後,設定選中第一項
            var val = $('#ddl_yearTime').combobox("getData");
            $("#ddl_yearTime").combobox("setValue", yyear);
        }
    });

ashx
/// <summary>
    /// 初始化年份型別
    /// </summary>
    /// <returns></returns>
    private string GetloadComYear()
    {
        string strRet = string.Empty; 
        string strdata = "[";
        for (int i = DateTime.Now.Year; i <= DateTime.Now.Year+1; i++)
        {
            strdata += "{\"id\":\"" + i + "上半年\",\"text\":\"" + i + "上半年\"},";

            if(i!=DateTime.Now.Year+1)
            strdata += "{\"id\":\"" + i + "下半年\",\"text\":\"" + i + "下半年\"},";
        }
        strdata = strdata.TrimEnd(',') + "]";
        strRet = strdata;

        return strRet;
    
    }