Dapatkan dropdown nilai yang dipilih di bootstrap

❮ 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 PropertiYaYaYaYaYa

Sintaksis

Kembalikan properti nilai

Tetapkan properti nilai

pilihObjek. nilai = nilai

Nilai properti

ValueDescriptionvalueMenentukan nilai suatu

Detail Teknis

Nilai Pengembalian. Sebuah String, mewakili nilai dari value atribut dari sebuah

Lebih Banyak Contoh

Contoh

Mengembalikan nilai opsi yang dipilih dalam daftar drop-down

var x = dokumen. getElementById("PilihSaya"). nilai;

Cobalah sendiri "


❮ Pilih Objek

Menu 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

    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

    • Dengan menggunakan properti value
    • Dengan menggunakan properti selectedIndex dengan properti option

    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

    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].value
    1
    selectElement.options[selectElement.selectedIndex].value
    2

    selectElement.options[selectElement.selectedIndex].value
    1
    selectElement.options[selectElement.selectedIndex].value
    4

        

    selectElement.options[selectElement.selectedIndex].value
    6title>

    selectElement.options[selectElement.selectedIndex].value
    6head>

     

    <________104

    selectElement.options[selectElement.selectedIndex].value
    _108_______

        <<!DOCTYPE html>8 <!DOCTYPE html>9 0 1>

    selectElement.options[selectElement.selectedIndex].value
    1 4

        

    selectElement.options[selectElement.selectedIndex].value
    6<!DOCTYPE html>8 8

        <<1>

    selectElement.options[selectElement.selectedIndex].value
    1<4

    selectElement.options[selectElement.selectedIndex].value
    1<6

        

    selectElement.options[selectElement.selectedIndex].value
    6<1>

        <head3head4

    selectElement.options[selectElement.selectedIndex].value
    1<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].value
    1
    selectElement.options[selectElement.selectedIndex].value
    6head7>

        

    selectElement.options[selectElement.selectedIndex].value
    6head3>

     

     

        <head3

    selectElement.options[selectElement.selectedIndex].value
    02

    selectElement.options[selectElement.selectedIndex].value
    1________106__2_______05
    selectElement.options[selectElement.selectedIndex].value
    06 0
    selectElement.options[selectElement.selectedIndex].value
    08
    selectElement.options[selectElement.selectedIndex].value
    09
    selectElement.options[selectElement.selectedIndex].value
    05>

        

    selectElement.options[selectElement.selectedIndex].value
    6head3>

     

     

        <

    selectElement.options[selectElement.selectedIndex].value
    20
    selectElement.options[selectElement.selectedIndex].value
    21 0
    selectElement.options[selectElement.selectedIndex].value
    23
    selectElement.options[selectElement.selectedIndex].value
    24
    selectElement.options[selectElement.selectedIndex].value
    20>

    selectElement.options[selectElement.selectedIndex].value
    _27

        <

    selectElement.options[selectElement.selectedIndex].value
    30
    selectElement.options[selectElement.selectedIndex].value
    31 0
    selectElement.options[selectElement.selectedIndex].value
    33>

        

    selectElement.options[selectElement.selectedIndex].value
    _36

    selectElement.options[selectElement.selectedIndex].value
    1
    selectElement.options[selectElement.selectedIndex].value
    38

    selectElement.options[selectElement.selectedIndex].value
    1
    selectElement.options[selectElement.selectedIndex].value
    40

    selectElement.options[selectElement.selectedIndex].value
    1
    selectElement.options[selectElement.selectedIndex].value
    42

        

    selectElement.options[selectElement.selectedIndex].value
    _44

        

    selectElement.options[selectElement.selectedIndex].value
    6
    selectElement.options[selectElement.selectedIndex].value
    30>

    selectElement.options[selectElement.selectedIndex].value
    6<!DOCTYPE html>4>

     

    selectElement.options[selectElement.selectedIndex].value
    6
    selectElement.options[selectElement.selectedIndex].value
    54>

    Keluaran

    Dapatkan dropdown nilai yang dipilih di bootstrap

    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 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].value
    1
    selectElement.options[selectElement.selectedIndex].value
    2

    selectElement.options[selectElement.selectedIndex].value
    1
    selectElement.options[selectElement.selectedIndex].value
    4

        

    selectElement.options[selectElement.selectedIndex].value
    6title>

    selectElement.options[selectElement.selectedIndex].value
    6head>

     

    <________104

    selectElement.options[selectElement.selectedIndex].value
    _108_______

        <<!DOCTYPE html>8 <!DOCTYPE html>9 0 1>

    selectElement.options[selectElement.selectedIndex].value
    1 4

        

    selectElement.options[selectElement.selectedIndex].value
    6<!DOCTYPE html>8 8

        <<1>

    selectElement.options[selectElement.selectedIndex].value
    1
    selectElement.options[selectElement.selectedIndex].value
    2

    selectElement.options[selectElement.selectedIndex].value
    1
    selectElement.options[selectElement.selectedIndex].value
    4

        

    selectElement.options[selectElement.selectedIndex].value
    6<1>

        <head3head4

    selectElement.options[selectElement.selectedIndex].value
    1<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].value
    1
    selectElement.options[selectElement.selectedIndex].value
    6head7>

        

    selectElement.options[selectElement.selectedIndex].value
    6head3>

        <head3

    selectElement.options[selectElement.selectedIndex].value
    02

    selectElement.options[selectElement.selectedIndex].value
    1<
    selectElement.options[selectElement.selectedIndex].value
    05
    selectElement.options[selectElement.selectedIndex].value
    06 0
    selectElement.options[selectElement.selectedIndex].value
    08
    selectElement.options[selectElement.selectedIndex].value
    09
    selectElement.options[selectElement.selectedIndex].value
    05 8

        

    selectElement.options[selectElement.selectedIndex].value
    6head3>

     

     

        <

    selectElement.options[selectElement.selectedIndex].value
    20
    selectElement.options[selectElement.selectedIndex].value
    21 0
    selectElement.options[selectElement.selectedIndex].value
    23<!DOCTYPE html>75
    selectElement.options[selectElement.selectedIndex].value
    20>

        <

    selectElement.options[selectElement.selectedIndex].value
    30
    selectElement.options[selectElement.selectedIndex].value
    31 0
    selectElement.options[selectElement.selectedIndex].value
    33>

     

        

    selectElement.options[selectElement.selectedIndex].value
    _36

    selectElement.options[selectElement.selectedIndex].value
    1
    selectElement.options[selectElement.selectedIndex].value
    38

    selectElement.options[selectElement.selectedIndex].value
    1<!DOCTYPE html>91

    selectElement.options[selectElement.selectedIndex].value
    1
    selectElement.options[selectElement.selectedIndex].value
    42

        

    selectElement.options[selectElement.selectedIndex].value
    _44

        

    selectElement.options[selectElement.selectedIndex].value
    6
    selectElement.options[selectElement.selectedIndex].value
    30>

    selectElement.options[selectElement.selectedIndex].value
    6<!DOCTYPE html>4>

     

    selectElement.options[selectElement.selectedIndex].value
    6
    selectElement.options[selectElement.selectedIndex].value
    54>

    Keluaran

    Dapatkan dropdown nilai yang dipilih di bootstrap

    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

    Bagaimana cara menunjukkan nilai yang dipilih dalam dropdown bootstrap?

    Anda perlu membungkus teks 'Pilih opsi' di dalam .

    Bagaimana cara mendapatkan nilai yang dipilih dari dropdown?

    Nilai elemen terpilih dapat ditemukan dengan menggunakan properti value pada elemen terpilih yang menentukan daftar . Properti ini mengembalikan string yang mewakili atribut nilai dari elemen

    Bagaimana cara mendapatkan nilai dari react bootstrap dropdown?

    Untuk mencapai ini, impor hook useState dari React. Panggil setValue di dalam fungsi handleSelect untuk menyetel variabel status ke nilai yang dipilih dari dropdown . Terakhir, tampilkan nilai status saat ini pada DOM di dalam JSX Anda.

    Bagaimana Anda menemukan nilai dari nilai yang dipilih?

    Untuk mengambil nilai yang dipilih dari elemen pilih, Anda dapat menggunakan prop event handler onChange . Sama seperti elemen input atau textarea, Anda bisa menggunakan event handler onChange untuk mendapatkan nilai dari objek event. Sekarang, buat elemen input pilihan ini dikontrol dengan menggunakan status untuk meneruskan nilainya.