html代碼:
<select name="ruleId" id="ruleId" class="required" onchange="selectRule()" multiple="multiple" class="form-control"> <c:forEach items="${fns:selectRuleListBycompany()}" var="item"> <option value="${item.id}" <c:if test="${record.ruleId == item.id}"> selected="selected" </c:if>>${item.ruleName} </option> </c:forEach> </select>
js獲取所有的值並放入到數組中
window.Garray = new Array(); //定義窗口全局變量數組
function selectRule(obj){ var selected = $("#ruleId").select2('data'); window.Garray = []; for (var i = 0; i < selected.length; i++) { Garray.push(selected[i].id); } }
$(function(){ var arrs = new Array(); var str = "${record.ruleIdList}"; arrs = str.replace('[','').replace(']','').replace(/\s/g,'').split(','); $("#ruleId").val(arrs).trigger("change"); });