1. 程式人生 > >JQuery select 程式設計時選中原有的值

JQuery select 程式設計時選中原有的值

js 此為核心程式碼

$(document).ready(function(){
$("#carTypeId").attr("value",'${carInfo.carTypeId}'); //後臺傳遞的值
})

 

<tr>
<th width="20%">車輛種類名稱: </th>
<td>
<%-- <input type="text" id="carTypeId" name="carTypeId" value="${carInfo.carTypeId}" class="inputText" validate="{required:false,number:true,maxIntLen:10}" />
--%>
<select name="carTypeId" id ="carTypeId" class="select panel-search-inputText">
<option value="">--選擇--</option>
<c:forEach items="${listCarType}" var="keyword" varStatus="id">
<option value="${keyword.id}" <c:if test="${keyword.id} == ${carInfo.carTypeId}">selected</c:if>> ${keyword.typeName} </option>
</c:forEach>
</select>
</td>

java 程式碼

List<CarType> listCarType=carTypeService.getAll();
return getAutoView().addObject("carInfo",carInfo !=null?carInfo:null)
.addObject("listCarType", listCarType)