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 + "'> " + item.name + "</option>"); }); /*設置預設值*/ $(customerId).val(values); } else { // hint(rsp.message); console.log(rsp); } }) } //使用 var customer = $("#customerId"); var val = data.customerId; htDate(customer, val);