layui下拉框js封装

/**
 * 初始化返回数据类型下拉框
 * @param comboboxId 下拉框ID
 * @param param 自定义sql要传入的参数,例如 {cfgCode:'ENUM_GETTER',enumCategoryId:enumCategoryId} 其中cfgCode为必传参数
 * @param needAll  true:代表显示全部
 * @param onSelectValue  默认需要选中的值,否则默认选中第一个
 */
function initSelect(comboboxId,param,needAll,onSelectValue){
    var url = path + "/app/signal/common/commonDataQuery!getDataByCfgCodeJustData.action";
    $.ajax( {
        type : 'post',
        url : url,
        data : param,
        async : true,
        success : function(data) {
            var arr = needAll === true ? [{"id":"","value":"","text":"全部"}] : [];
            arr=arr.concat($.parseJSON(data));
            var $html = "";
            for(var i=0;i<arr.length;i++){
                if(onSelectValue && arr[i].value==onSelectValue){
                    $html += "<option value='" + arr[i].value + "' selected>" + arr[i].text + "</option>";
                }else{
                    $html += "<option value='" + arr[i].value + "'>" + arr[i].text + "</option>";
                }
            }
            $("#"+comboboxId).append($html);
        }
    });
}

猜你喜欢

转载自blog.csdn.net/qq_22165667/article/details/82253569
今日推荐