asp学习网: 首页 >> javascript教程 >> javascript基础 >> javascript参考 表单元素的属性和方法 一

javascript参考 表单元素的属性和方法 一

 

Option
An option in a selection list.


参数
text (Optional) Specifies the text to display in the select list.
value (Optional) Specifies a value that is returned to the server when the option is selected and the form is submitted.
defaultSelected (Optional) Specifies whether the option is initially selected (true or false).
selected (Optional) Specifies the current selection state of the option (true or false).

属性
defaultSelected
A Boolean value indicating the default selection state of an option in a selection list.
属性源 Option


描述
If an option is selected by default, the value of the defaultSelected property is true; otherwise, it is false. defaultSelected initially reflects whether the SELECTED attribute is used within an OPTION tag; however, setting defaultSelected overrides the SELECTED attribute.
You can set the defaultSelected property at any time. The display of the corresponding Select object does not update when you set the defaultSelected property of an option, only when you set the Option.selected or Select.selectedIndex properties.

A Select object created without the MULTIPLE attribute can have only one option selected by default. When you set defaultSelected in such an object, any previous default selections, including defaults set with the SELECTED attribute, are cleared. If you set defaultSelected in a Select object created with the MULTIPLE attribute, previous default selections are not affected.


示例
In the following example, the restoreDefault function returns the musicType Select object to its default state. The for loop uses the options array to evaluate every option in the Select object. The if statement sets the selected property if defaultSelected is true.
function restoreDefault() {
for (var i = 0; i < document.musicForm.musicType.length; i++) {
if (document.musicForm.musicType.options[i].defaultSelected == true) {
document.musicForm.musicType.options[i].selected=true
}
}
} The previous example assumes that the Select object is similar to the following:

<SELECT NAME="musicType">
<OPTION SELECTED> R&B
<OPTION> Jazz
<OPTION> Blues
<OPTION> New Age
</SELECT>


selected
A Boolean value indicating whether an option in a Select object is selected.
属性源 Option

描述
If an option in a Select object is selected, the value of its selected property is true; otherwise, it is false. You can set the selected property at any time. The display of the associated Select object updates immediately when you set the selected property for one of its options.
In general, the Option.selected property is more useful than the Select.selectedIndex property for Select objects that are created with the MULTIPLE attribute. With the Option.selected property, you can evaluate every option in the Select.options array to determine multiple selections, and you can select individual options without clearing the selection of other options.


text
A string specifying the text of an option in a selection list.
属性源 Option


描述
The text property initially reflects the text that follows an OPTION tag of a SELECT tag. You can set the text property at any time and the text displayed by the option in the selection list changes.

示例 
In the following example, the getChoice function returns the value of the text property for the selected option. The for loop evaluates every option in the musicType Select object. The if statement finds the option that is selected.
function getChoice() {
for (var i = 0; i < document.musicForm.musicType.length; i++) {
if (document.musicForm.musicType.options[i].selected == true) {
return document.musicForm.musicType.options[i].text
}
}
return null
} The previous example assumes that the Select object is similar to the following:

<SELECT NAME="musicType">
<OPTION SELECTED> R&B
<OPTION> Jazz
<OPTION> Blues
<OPTION> New Age
</SELECT>

value
A string that reflects the VALUE attribute of the option.
属性源 Option


描述
When a VALUE attribute is specified in HTML, the value property is a string that reflects it. When a VALUE attribute is not specified in HTML, the value property is the empty string. The value property is not displayed on the screen but is returned to the server if the option is selected.
Do not confuse the property with the selection state of the option or the text that is displayed next to it. The selected property determines the selection state of the object, and the defaultSelected property determines the default selection state. The text that is displayed is specified following the OPTION tag and corresponds to the text property.

 

from:asp学习网/title:javascript参考 表单元素的属性和方法 一/ time:2007-3-30 19:47:27

本文主题表单

语义化——表单

ASp表单项目的数据传送的例子

使用快捷键提交表单的方法

设计个性化表单的五个技巧

表单中使用颜色选择器(IE)

利用userData实现客户端保存表单数据

表单元件在说什么?---分析表单元件的语义

常用表单格式的五点技巧

asp教程 ©2006-2007 aspxuexi.com | 关于站点 | 版权隐私 | 站内搜索
复制或者翻版 请于夜间进行