❮ Pilih Objek
Contoh
Ubah opsi yang dipilih menjadi "pisang"
dokumen. getElementById("PilihSaya"). nilai = "pisang";
Cobalah sendiri "Definisi dan Penggunaan
Properti value menetapkan atau mengembalikan nilai opsi yang dipilih dalam daftar drop-down
Dukungan Peramban
Nilai PropertiYaYaYaYaYaSintaksis
Kembalikan properti nilai
Tetapkan properti nilai
pilihObjek. nilai = nilai
Nilai properti
ValueDescriptionvalueMenentukan nilai suatuDetail Teknis
Nilai Pengembalian. Sebuah String, mewakili nilai dari value atribut dari sebuahLebih Banyak Contoh
Contoh
Mengembalikan nilai opsi yang dipilih dalam daftar drop-down
var x = dokumen. getElementById("PilihSaya"). nilai;
Cobalah sendiri "❮ Pilih ObjekMenu tarik-turun adalah menu yang dapat dialihkan yang memungkinkan pengguna memilih satu nilai dari daftar yang telah ditentukan sebelumnya
Contoh
Contoh Turun
Cobalah sendiri "
Contoh Dijelaskan
Kelas .dropdown_ menunjukkan menu tarik-turun
Untuk membuka menu tarik-turun, gunakan tombol atau tautan dengan kelas .dropdown-toggle dan atribut data-toggle="dropdown"
Kelas .caret_ membuat ikon tanda sisipan (), yang menunjukkan bahwa tombol tersebut adalah dropdown
Tambahkan kelas .dropdown-menu ke a
- elemen untuk benar-benar membangun menu dropdown
- HTML
- CSS
- JavaScript
- Dengan menggunakan properti value
- Dengan menggunakan properti selectedIndex dengan properti option
- selectedIndex: It is used to set or get the index of the selected element in the collection.
- length: It is read-only property that is used to get the number of elements in the collection.
Pemisah Dropdown
Kelas ________ 97 ______ digunakan untuk memisahkan tautan di dalam menu dropdown dengan batas horizontal tipis
Tajuk Turun
Kelas ________ 98 ______ digunakan untuk menambahkan header di dalam menu dropdown
Nonaktifkan dan item Aktif
Sorot item dropdown tertentu dengan. kelas aktif (menambahkan warna latar belakang biru)
Untuk menonaktifkan item di menu tarik-turun, gunakan kelas .disabled (mendapatkan warna teks abu-abu muda dan ikon "tanda dilarang parkir" saat mengarahkan kursor)
Contoh
Cobalah sendiri "
Posisi Turun
Untuk menyelaraskan dropdown, tambahkan kelas .dropdown-menu-right ke elemen dengan. menu drop down
Dropup
Jika Anda ingin menu dropdown melebar ke atas, bukan ke bawah, ubah
elemen dengan class="dropdown" ke .dropdown0
Aksesibilitas Drop-down
Untuk membantu meningkatkan aksesibilitas bagi orang yang menggunakan pembaca layar, Anda harus menyertakan atribut .dropdown1 dan .dropdown2 berikut, saat membuat menu tarik-turun
Contoh
Tutorial
Cobalah sendiri "
Uji Diri Anda Dengan Latihan
Latihan
Tambahkan kelas dan atribut yang diperlukan untuk membuat daftar tarik-turun
Dropdown Example
Kirim Jawaban »
Mulai Latihan
Referensi Dropdown Bootstrap Lengkap
Untuk referensi lengkap semua opsi dropdown, metode, dan event, buka Referensi Dropdown Bootstrap JS kami
Pada artikel ini, kita akan belajar mendapatkan nilai yang dipilih dalam daftar dropdown di Javascript. Kita bisa mendapatkan nilai menggunakan 2 metode
Kami akan memahami kedua metode ini melalui contoh
Metode 1. Menggunakan properti nilai.
The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the element in the list. If no option is selected then nothing will be returned.
Sintaksis
selectElement.value_Contoh. Contoh ini menjelaskan properti nilai yang dapat ditemukan untuk elemen yang dipilih
HTML
<!DOCTYPE html>
<head>
<title>
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value2
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value4
selectElement.options[selectElement.selectedIndex].value6title>
selectElement.options[selectElement.selectedIndex].value6head>
<________104selectElement.options[selectElement.selectedIndex].value_108_______
<<!DOCTYPE html>8 <!DOCTYPE html>9 0 1>
selectElement.options[selectElement.selectedIndex].value1 4
selectElement.options[selectElement.selectedIndex].value6<!DOCTYPE html>8 8
<<1>
selectElement.options[selectElement.selectedIndex].value1<4
selectElement.options[selectElement.selectedIndex].value1<6
selectElement.options[selectElement.selectedIndex].value6<1>
<head3head4
selectElement.options[selectElement.selectedIndex].value1<head7 head8 0>0>
>2<>4 >5 0>7>8>4>
>2<>4 ________108______5 0 6________109______7>4>
>2<>4 ________108______5 0<5<6>4>
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value6head7>
selectElement.options[selectElement.selectedIndex].value6head3>
<head3selectElement.options[selectElement.selectedIndex].value02
selectElement.options[selectElement.selectedIndex].value1________106__2_______05 selectElement.options[selectElement.selectedIndex].value06 0selectElement.options[selectElement.selectedIndex].value08selectElement.options[selectElement.selectedIndex].value09selectElement.options[selectElement.selectedIndex].value05>
selectElement.options[selectElement.selectedIndex].value6head3>
<selectElement.options[selectElement.selectedIndex].value20 selectElement.options[selectElement.selectedIndex].value21 0selectElement.options[selectElement.selectedIndex].value23selectElement.options[selectElement.selectedIndex].value24selectElement.options[selectElement.selectedIndex].value20>
selectElement.options[selectElement.selectedIndex].value_27
<selectElement.options[selectElement.selectedIndex].value30 selectElement.options[selectElement.selectedIndex].value31 0selectElement.options[selectElement.selectedIndex].value33>
selectElement.options[selectElement.selectedIndex].value_36
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value38
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value40
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value42
selectElement.options[selectElement.selectedIndex].value_44
selectElement.options[selectElement.selectedIndex].value6selectElement.options[selectElement.selectedIndex].value30>
selectElement.options[selectElement.selectedIndex].value6<!DOCTYPE html>4>
selectElement.options[selectElement.selectedIndex].value6selectElement.options[selectElement.selectedIndex].value54>
Keluaran
properti nilai
Metode 2. Menggunakan properti selectedIndex dengan properti option
The selectedIndex property returns the index of the currently selected element in the dropdown list. This index starts from 0 and returns -1 if no option is selected. The options property returns the collection of all the option elements in the dropdown list. The elements are sorted according to the source code of the page. The index found before it can be used with this property to get the selected element. This option’s value can be found by using the value property.
Sintaksis
selectElement.options[selectElement.selectedIndex].valueNilai properti
Return value: It returns HTMLOptionsCollection Object by specifying all the elements in the element. The element will be sorted in the collection
Contoh. Contoh ini menjelaskan properti selectedIndex dengan properti option
HTML
<!DOCTYPE html>
<head>
<title>
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value2
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value4
selectElement.options[selectElement.selectedIndex].value6title>
selectElement.options[selectElement.selectedIndex].value6head>
<________104selectElement.options[selectElement.selectedIndex].value_108_______
<<!DOCTYPE html>8 <!DOCTYPE html>9 0 1>
selectElement.options[selectElement.selectedIndex].value1 4
selectElement.options[selectElement.selectedIndex].value6<!DOCTYPE html>8 8
<<1>
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value2
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value4
selectElement.options[selectElement.selectedIndex].value6<1>
<head3head4
selectElement.options[selectElement.selectedIndex].value1<head7 head8 0>0>
>2<>4 >5 0>7>8>4>
>2<>4 ________108______5 0 6________109______7>4>
>2<>4 ________108______5 0<5<6>4>
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value6head7>
selectElement.options[selectElement.selectedIndex].value6head3>
<head3selectElement.options[selectElement.selectedIndex].value02
selectElement.options[selectElement.selectedIndex].value1<selectElement.options[selectElement.selectedIndex].value05 selectElement.options[selectElement.selectedIndex].value06 0selectElement.options[selectElement.selectedIndex].value08selectElement.options[selectElement.selectedIndex].value09selectElement.options[selectElement.selectedIndex].value05 8
selectElement.options[selectElement.selectedIndex].value6head3>
<selectElement.options[selectElement.selectedIndex].value20 selectElement.options[selectElement.selectedIndex].value21 0selectElement.options[selectElement.selectedIndex].value23<!DOCTYPE html>75selectElement.options[selectElement.selectedIndex].value20>
<selectElement.options[selectElement.selectedIndex].value30 selectElement.options[selectElement.selectedIndex].value31 0selectElement.options[selectElement.selectedIndex].value33>
selectElement.options[selectElement.selectedIndex].value_36
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value38
selectElement.options[selectElement.selectedIndex].value1<!DOCTYPE html>91
selectElement.options[selectElement.selectedIndex].value1selectElement.options[selectElement.selectedIndex].value42
selectElement.options[selectElement.selectedIndex].value_44
selectElement.options[selectElement.selectedIndex].value6selectElement.options[selectElement.selectedIndex].value30>
selectElement.options[selectElement.selectedIndex].value6<!DOCTYPE html>4>
selectElement.options[selectElement.selectedIndex].value6selectElement.options[selectElement.selectedIndex].value54>
Keluaran
Properti SelectedIndex dengan properti opsi
JavaScript terkenal untuk pengembangan halaman web tetapi juga digunakan di berbagai lingkungan non-browser. Anda dapat mempelajari JavaScript dari awal dengan mengikuti Tutorial JavaScript dan Contoh JavaScript ini