|
【目录】 【上一页】 【下一章】 【索引】
OptionAn option in a selection list.
创建源The Option constructor or the HTML OPTION tag. To create an Option object with its constructor:new Option(text, value, defaultSelected, selected) Once you've created an Option object, you can add it to a selection list using the Select.options array. 参数
属性概览
描述Usually you work with Option objects in the context of a selection list (a Select object). When JavaScript creates a Select object for each SELECT tag in the document, it creates Option objects for the OPTION tags inside the SELECT tag and puts those objects in the options array of the Select object.In addition, you can create new options using the Option constructor and add those to a selection list. After you create an option and add it to the Select object, you must refresh the document by using history.go(0). This statement must be last. When the document reloads, variables are lost if not saved in cookies or form element values. You can use the Option.selected and Select.selectedIndex properties to change the selection state of an option.
document.myForm.musicTypes.selectedIndex = i document.myForm.musicTypes.options[i].selected = true
<SELECT name="userChoice"> myform.userChoice.options[i].text = myform.whatsNew.value You do not need to reload or refresh after changing an option's text. 示例The following example creates two Select objects, one with and one without the MULTIPLE attribute. No options are initially defined for either object. When the user clicks a button associated with the Select object, the populate function creates four options for the Select object and selects the first option.
<SCRIPT> 属性defaultSelectedA Boolean value indicating the default selection state of an option in a selection list.
安全性Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”。描述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() {
<SELECT NAME="musicType"> 参看Option.selected, Select.selectedIndexselectedA Boolean value indicating whether an option in a Select object is selected.
安全性Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”。描述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. 示例See the示例 for defaultSelected.参看Option.defaultSelected, Select.selectedIndextextA string specifying the text of an option in a selection list.
安全性Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”。描述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.示例示例 1. 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() {
<SELECT NAME="musicType">
The code for this example looks as follows:
<SCRIPT> 参看getOptionValuevalueA string that reflects the VALUE attribute of the option.
安全性Navigator 3.0:该属性默认是带有污点的。有关数据污点的更多信息,请看“JavaScript 的安全性”。描述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.
【目录】 【上一页】 【下一章】 【索引】 |