Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the options collection in javascript

Shulou Source: shulou.com Published: 2022-05-31 11:02:31 11月13日 Update

This article mainly explains "how to use the options collection in javascript". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the options collection in javascript".

The object.options.add (new Option (label,value)) method adds an option object to the collection

The object.options.remove (index) method removes the specified item from the options collection

Object.options (index) or options.item (index) can get the specified items of the options collection through the index

The select tag also has an attribute of selectedIndex, which makes it possible to get the currently selected option index: object.selectedIndex

1. Clear the options collection

Code highlighting produced by Actipro CodeHighlighter (freeware)

Http://www.CodeHighlighter.com/

-& gtfunction optionsClear (object)

{

Var length = object.options.length

For (var iMUTHUTHUTHUTHI > = 0PUBILUR -) {

E.options.remove (I)

}

}

two。 Add a new option

Code highlighting produced by Actipro CodeHighlighter (freeware)

Http://www.CodeHighlighter.com/

-& gtfunction addOption (object)

{

Object.add (new Option (label,value))

/ / use the last item in the options collection to get focus

Object.selectedIndex = object.lentht-1

}

3. Delete an option specified in the options collection

Code highlighting produced by Actipro CodeHighlighter (freeware)

Http://www.CodeHighlighter.com/

-& gtfunction removeOption (index)

{

If (index > = 0)

{

Object.remove (index)

/ / use the last item in the options collection to get focus

Object.selectedIndex = object.lentht-1

}

}

4. Gets the real value value of the currently selected option

Code highlighting produced by Actipro CodeHighlighter (freeware)

Http://www.CodeHighlighter.com/

-& gtfunction getCurrentOptionValue (index)

{

If (index > = 0)

Return object (index). Value

}

5. Gets the display value label of the currently selected option

Code highlighting produced by Actipro CodeHighlighter (freeware)

Http://www.CodeHighlighter.com/

-& gtfunction getCurrentOptionLabel (index)

{

If (index > = 0)

Return object (index). Text

}

Thank you for your reading, the above is the content of "how to use the options collection in javascript". After the study of this article, I believe you have a deeper understanding of how to use the options collection in javascript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Learning content method focus index plus one middle finger can pass object that is attributes ideas situations articles more tags knowledge knowledge points articles follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information Apple Linux MariaDB