Select Object

Select Object គឺ​ជា​ធាតុ HTML ម្យ៉ាង​ប្រើ​សំរាប់​ផ្តល់​ជំរើស​ជា​ច្រើន​ដល់​អ្នក​ប្រើប្រាស់​។ យើង​អាច​ប្រើ​វិធី​ផ្សេង​ៗ​នៅ​ក្នុង DOM ក្នុង​ការដោះដូរ​ឬ​ចំលង​យក​ព័ត៌មាន​ពី​អ្វី​ៗ​មាន​នៅ​ក្នុង​ធាតុ HTML ដែល​មាន​ប្រភេទ​ជា select នេះ​ ដោយ​សរសេរ​កម្មវិធី​ដូច​ខាង​ក្រោម​៖
<!DOCTYPE html>
<html>
  <head>
    <link href='https://fonts.googleapis.com/css?family=Odor+Mean+Chey' rel='stylesheet'>
  </head>
  <body style="font:14px/1.5 'Odor Mean Chey'">
  	
    

ចុច​ជ្រើស​រើស​យក​ផ្លែ​ឈើ​ដែល​អ្នក​ពេញ​ចិត្ត។

<script> function myFunction(){ var selectObject = document.getElementById("mySelect"); var element = document.getElementById("info"); element.innerHTML = "អ្នក​បាន​ជ្រើសរើស​យក: " + selectObject.value; } </script> </body> </html>

ចុច​ជ្រើស​រើស​យក​ផ្លែ​ឈើ​ដែល​អ្នក​ពេញ​ចិត្ត។


នៅ​លើ​បន្ទាត់​លេខ 23 គឺ​ជាការយក​វិធី getElementById() នៅ​ក្នុង DOM មក​ប្រើ​ដើម្បី​ភ្ជាប់​ទំនាក់ទំនង​ទៅ​នឹង​ធាតុ select ដែល​មាន​អត្តសញ្ញាណ​ជា mySelect ។

នៅ​លើ​បន្ទាត់​លេខ 24 គឺ​ជាការយក​វិធី getElementById() នៅ​ក្នុង DOM មក​ប្រើ​ដើម្បី​ភ្ជាប់​ទំនាក់ទំនង​ទៅ​នឹង​ធាតុ HTML ដែល​មាន​អត្តសញ្ញាណ​ជា info ។

នៅ​លើ​បន្ទាត់​លេខ 25 ការសរសេរ​ថា selectObject.value គឺ​ជា​ការចំលង​យក​ទិន្នន័យ​ដែល​ជា​ជំរើស​របស់​អ្នក​ប្រើប្រាស់​នៅ​ក្នុង​ធាតុ select មក​បញ្ចូល​ទៅ​ក្នុង​ធាតុ HTML ដែល​មាន​អត្តសញ្ញាណ​ជា info ។

Select Object Properties & Methods
Property/Method Description
autofocus Sets or returns whether a drop-down list should automatically get focus when the page loads
disabled Sets or returns whether the drop-down list is disabled, or not
form Returns a reference to the form that contains the drop-down list
length Returns the number of <option> elements in a drop-down list
multiple Sets or returns whether more than one option can be selected from the drop-down list
name Sets or returns the value of the name attribute of a drop-down list
selectedIndex Sets or returns the index of the selected option in a drop-down list
size Sets or returns the value of the size of a drop-down list
type Returns which type of form element a drop-down list is
value Sets or returns the value of the selected option in a drop-down list
add() Adds an option to a drop-down list
remove() Removes an option from a drop-down list