一個很奇怪的現象,下拉框的selected屬性如果爲空,那麼默認選中項將失去該有的功能,舉例:
<select id="machRoom">
<option id= "1" selected="">sdf11</option>
<option id= "2" selected="">sdf22</option>
<option id= "4" selected="selected">sdf000</option>
<option id= "3" selected="">sdf33</option>
</select>
比如上面,將沒有默認項,原因selected屬性只有爲”selected“時,默認選中,當爲“”時,則屬性失效。這就意味着我從後臺的代碼錯誤正是如此:
機房:<select id="machRoom" οnchange="showDev();">
<c:forEach items="${roomList}" var="list" >
<option value="${list.key}" selected="${list.key==flag1}?'selected':''">${list.value} </option>
</c:forEach>
</select>
原因:當沒有選中的項循環時,給selected屬性賦值爲“”,故而錯誤。
當瞭解到<option id= "4" selected>sdf000</option>這種方式也能默認選項時,我笑了,迅解決如下:
機房:<select id="machRoom" >
<c:forEach items="${roomList}" var="list" >
<option value="${list.key}" ${list.key==flag1?'selected':''}>${list.value}</option>
</c:forEach>
</select>
flag1爲後臺默認要選中的選項。這是就避免了selected=“”時的情況。
附:個人qq空間:http://user.qzone.qq.com/449125551/profile/sync#!app=2&via=QZ.HashRefresh&pos=1384014434