選擇
選 擇 物 件 即 是 HTML 的 <select> 標 記 , 而 這 個 標 記 內 的 <option> 標 記 就 稱 為 選 項 。
方法
屬性
事件
select
blur() focus()
length name options[ ] selectedIndex
onBlur onFocus onChange
例 子 : 檢 查 Select 物 件 的 一 個 選 項 ( 通 常 用 於 非 multiple select)
<form> <select name="interest" size="1" onChange="changed (this )"> <option selected value="int0">Computing</option> <option value="int1">Music</option> <option value="int2">Sports</option> </select> </form> <script> function changed(theselect ) { alert(theselect.value) } </script>
例 子 : 檢 查 Select 物 件 的 所 有 選 項 ( 通 常 用 於 multiple select)
<form> <select name="interest" size="7" multiple onChange="changed (this )"> <option selected value="int0">Computing</option> <option value="int1">Music</option> <option value="int2">Sports</option> </select> </form> <script> function changed(theselect ) { var output="" var id = theselect.selectedIndex var thename = theselect.name var num_options = theselect.length output += "selectedIndex = " + id + "\n" output += "Your " + thename + " is " + theselect.options[id].text alert(output) } </script>
<form>
<select name="interest" size="7" onChange="changed(this)">
<option selected value="int0">Computing</option>
<option value="int1">Music</option>
<option value="int2">Sports</option>
</select>
</form> <script>
function changed(theselect) {
var output=""
var id = theselect.selectedIndex
var thename = theselect.name
var num_options = theselect.length
output += "selectedIndex = " + id + "\n"
output += "Your " + thename + " is " + theselect.options[id].text
alert(output)
}
</script>
例 子 說 明 :
如 果 Select 物 件 沒 有 任 何 Option 被 選 取 , selectedIndex 屬 性 就 等 於 負 一 (-1) 。
option
defaultSelected
是否預設的選項, 選擇項目 HTML 標記的 selected 屬性 (true / false)
selected
是否被瀏覽者選取 (true / false)
text
選項的顯示文字
value
選項的值
你 也 可 以 自 行 產 生 Option 物 件 , 以 修 改 頁 內 的 Option 。
語 法 :
var Option_var = new Option(text , value , defaultSelected , selected )
說 明 :
text
選項的顯示文字
value
選 項 的 值
defaultSelected
是否預設的選項 (true / false)
selected
是否替瀏覽者選取 (true / false)
例 子 : 將 自 訂 選 項 加 入 到 Select
<form> <select name="interest" size="7"> </select> <input type="button" value="Add Options" onclick="add(this.form.interest)"> </form> <script> function add(theselect) { var textarray = new Array("Computing","Music","Sports") var valuearray = new Array("int0","int1","int2") for (i = 0 ; i < textarray.length ; i++) { theselect.options[theselect.length] = new Option (textarray[i] , valuearray[i]) } } </script>
<form>
<select name="interest" size="7">
</select>
<input type="button" value="Add Options" onclick="add(this.form.interest)">
</form> <script>
function add(theselect) { var textarray = new Array("Computing","Music","Sports") var valuearray = new Array("int0","int1","int2") for (i = 0 ; i < textarray.length ; i++) { theselect.options[theselect.length] = new Option(textarray[i] , valuearray[i]) }
}
</script>
例 子 說 明 :
例 子 : 刪 除 Select 內 某 個 Option
Select an option and delete it. <form> <select name="interest" size="7"> <option value="int0">Computing</option> <option value="int1">Music</option> <option value="int2">Sports</option> </select> <input type="button" value="Delete Options" onclick="del(this.form.interest)"> </form> <script> function del(theselect) { var id = theselect.selectedIndex if (id != -1) { for (i = id ; i < theselect.length -1 ; i++) { // For NN, you can use theselect.options[i] = theselect.options[i+1] optext = theselect.options[i+1].text opvalue = theselect.options[i+1].value opdefault = theselect.options[i+1].defaultSelected opselected = theselect.options[i+1].selected theselect.options[i] = new Option(optext, opvalue, opdefault, opselected) } theselect.length -- } } </script>
Select an option and delete it.
<form>
<select name="interest" size="7">
<option value="int0">Computing</option>
<option value="int1">Music</option>
<option value="int2">Sports</option>
</select>
<input type="button" value="Delete Options" onclick="del(this.form.interest)">
</form>
<script>
function del(theselect) {
var id = theselect.selectedIndex
if (id != -1) { for (i = id ; i < theselect.length -1 ; i++) { // For Netscape, you can use theselect.options[i] = theselect.options[i+1] optext = theselect.options[i+1].text opvalue = theselect.options[i+1].value opdefault = theselect.options[i+1].defaultSelected opselected = theselect.options[i+1].selected theselect.options[i] = new Option(optext, opvalue, opdefault, opselected) } theselect.length --
}
}
</script>
例 子 說 明 :
2005-10-26 18:09:42
Posted by jser | 阅读全文( ) | 回复(0) | 引用通告( ) | 编辑
..................................................
博客的精神于写自己某一刻的思想或心动!
..................................................
当然要是你愿意就是拿来做记事本也可以!
..................................................
群落博客将提供全程免费服务!免费注册!
..................................................
欢迎您成为群落博客的成员!使用之前请先看系统帮助>>>>系统帮助
..................................................
免费注册群落博客【点击完成注册】
..................................................
发表评论: