select和oprtion並設置默認選項
[color=blue]js構造select和oprtion並設置默認選項[/color]
如果是手寫 html select ,那麼很容易構造出來默認選中的選項,方法是:
<[color=olive]option value=’beiing’ selected > 北京 </option> [/color]
如果是 struts 的 select:option 標籤,那麼有 value 可以控制默認選中的標籤,方法:
[color=green]< html:select property = "compId" styleClass = "formclass" style = "width:80%" value = "xxx " > [/color]
如果是自己用 js 構造的 select ,那麼 Option 默認選中的選項不太好用。看了看 Option 的源碼,好像是標準還不支持,源碼居然只處理 4 個參數中的前兩個: function Option( text, value , defaultSelected, selected){};
對 defaultSelected, selected 兩個參數不管。
而 select 中的 selected 和 value 也不管用。
this .orderCreateModeSelect. selected = "3" ; //lijg 3:auto// 不可行
this .orderCreateModeSelect. value = "3" ; //lijg 3:auto// 不可行
因此只能用循環來處理了,方法:
/**
params: selectUi, selectedValue
result: if the selectedValue is not single, then select the first one.
author: lijg 2009-10-10
*/
Toolkit.selectItem = function (selectUi, selectedValue) {
if (selectUi == null || selectedValue == null ) return ;
for ( var i = 0; i< selectUi.options.length; i++) {
if (selectUi.options[i].value == selectedValue) {
selectUi.options[i].selected = true ; // 可行
//this.orderCreateModeSelect.selectedIndex = i;// 可行
break ;
}
}
}
在需要的地方調用此方法:
Toolkit.selectItem( this .orderCreateModeSelect, "3" ); //set default we need
附註:
Struts html:select 標籤顯示默認選項
( 1 ) http://www.iteye.com/topic/36716
最近在寫一個系統中,有一個有下拉列表的修改資料功能,對 Struts 的 html:select 進行了運用。
其中怎麼樣使進入修改頁面之後,該下拉列表裏的數據是該需要修改的數據列的數據字段,也就是使 html:select 中顯示的值默認對應數據庫中的值。測試修改了一個下午,總得不出結果,最後還是看了 Struts 的 examples 纔算是弄出來了
< html:select property = "id" >
<!-- 這裏一定不能再用 vlue 屬性了,只用一個 property 就可以了 -->
< c:forEach var = "row" item = "${rs.rows}" >
< html:option value = "row.id" >
< c:out value = "row.name" />
</ html:option >
</ c:forEach >
</ html:select >
總結:在使用 Struts 進行修改功能時,要在修改頁面上取的數據庫先前的值。只要在 form 中 reset 裏取得數據庫裏的值,然後在前臺頁面裏 html:form 裏對應的項設置其 property 爲 form 中對應的屬性值就好了,而不需要再設置 value 屬性了。
( 2 )如果運用了 < html:options> 標籤,默認選項在 value 處標明
< html:select property = "compId" styleClass = "formclass" style = "width:80%" value = "xxx " >
< html:options collection = "compList" property = "deptId" labelProperty = "deptName" />
</ html:select >
JQUERY中怎麼設置select中的默認選項啊
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$( document ).ready( function(){
//2,3設置成默認
$( 'select' ).val( Array( '2', '3' ) );
} );
</script>
<select multiple="multiple">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
如果是手寫 html select ,那麼很容易構造出來默認選中的選項,方法是:
<[color=olive]option value=’beiing’ selected > 北京 </option> [/color]
如果是 struts 的 select:option 標籤,那麼有 value 可以控制默認選中的標籤,方法:
[color=green]< html:select property = "compId" styleClass = "formclass" style = "width:80%" value = "xxx " > [/color]
如果是自己用 js 構造的 select ,那麼 Option 默認選中的選項不太好用。看了看 Option 的源碼,好像是標準還不支持,源碼居然只處理 4 個參數中的前兩個: function Option( text, value , defaultSelected, selected){};
對 defaultSelected, selected 兩個參數不管。
而 select 中的 selected 和 value 也不管用。
this .orderCreateModeSelect. selected = "3" ; //lijg 3:auto// 不可行
this .orderCreateModeSelect. value = "3" ; //lijg 3:auto// 不可行
因此只能用循環來處理了,方法:
/**
params: selectUi, selectedValue
result: if the selectedValue is not single, then select the first one.
author: lijg 2009-10-10
*/
Toolkit.selectItem = function (selectUi, selectedValue) {
if (selectUi == null || selectedValue == null ) return ;
for ( var i = 0; i< selectUi.options.length; i++) {
if (selectUi.options[i].value == selectedValue) {
selectUi.options[i].selected = true ; // 可行
//this.orderCreateModeSelect.selectedIndex = i;// 可行
break ;
}
}
}
在需要的地方調用此方法:
Toolkit.selectItem( this .orderCreateModeSelect, "3" ); //set default we need
附註:
Struts html:select 標籤顯示默認選項
( 1 ) http://www.iteye.com/topic/36716
最近在寫一個系統中,有一個有下拉列表的修改資料功能,對 Struts 的 html:select 進行了運用。
其中怎麼樣使進入修改頁面之後,該下拉列表裏的數據是該需要修改的數據列的數據字段,也就是使 html:select 中顯示的值默認對應數據庫中的值。測試修改了一個下午,總得不出結果,最後還是看了 Struts 的 examples 纔算是弄出來了
< html:select property = "id" >
<!-- 這裏一定不能再用 vlue 屬性了,只用一個 property 就可以了 -->
< c:forEach var = "row" item = "${rs.rows}" >
< html:option value = "row.id" >
< c:out value = "row.name" />
</ html:option >
</ c:forEach >
</ html:select >
總結:在使用 Struts 進行修改功能時,要在修改頁面上取的數據庫先前的值。只要在 form 中 reset 裏取得數據庫裏的值,然後在前臺頁面裏 html:form 裏對應的項設置其 property 爲 form 中對應的屬性值就好了,而不需要再設置 value 屬性了。
( 2 )如果運用了 < html:options> 標籤,默認選項在 value 處標明
< html:select property = "compId" styleClass = "formclass" style = "width:80%" value = "xxx " >
< html:options collection = "compList" property = "deptId" labelProperty = "deptName" />
</ html:select >
JQUERY中怎麼設置select中的默認選項啊
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$( document ).ready( function(){
//2,3設置成默認
$( 'select' ).val( Array( '2', '3' ) );
} );
</script>
<select multiple="multiple">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.