欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

select2 插件编辑时设置默认值

程序员文章站 2022-07-02 17:47:28
function htDate(selectCustomerId, val) { var customerId = selectCustomerId; var values = val; ajaxJson('GET', webroot + "/customer/getOptionList", '',... ......
function htDate(selectCustomerId, val) {
    var customerId = selectCustomerId;
    var values = val;
    ajaxJson('GET', webroot + "/customer/getOptionList", '', function(err, rsp) {
        if (rsp.code == 200) {
            var text = rsp.result;
            customerId.select2({
                placeholder: "请选择客户",
                allowClear: true,
                language: "zh-CN",
                width: '410px'
                    // data:text,
            });

            //绑定Ajax的内容
            customerId.empty(); //清空下拉框
            $.each(text, function(i, item) {
                customerId.append("<option value='" + item.id + "'>&nbsp;" + item.name + "</option>");
            });
            /*设置默认值*/
            $(customerId).val(values);
        } else {
            // hint(rsp.message);
            console.log(rsp);
        }
    })
}
//使用
var customer = $("#customerId");
var val = data.customerId;
htDate(customer, val);