Javascript periksa apakah kotak kombo dipilih

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

    <________98__95_______

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].value6________94__95_______

 

<<!DOCTYPE html>4>

    <<!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 >5 0    6    7>4>

>2<>4 >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________93__1_______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].value_1selectElement.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].value_

Nilai properti

  • 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.

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>

    <________98__95_______

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].value6________94__95_______

 

<<!DOCTYPE html>4>

    <<!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 >5 0    6    7>4>

>2<>4 >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].value_1selectElement.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

Bagaimana cara mengetahui apakah suatu opsi dipilih dalam JavaScript?

Sebuah opsi dianggap dipilih jika memiliki HTMLOptionElement. atribut yang dipilih .

Bagaimana cara mendapatkan nilai ComboBox yang dipilih dalam JavaScript?

Dapatkan Nilai Kotak Kombo yang Dipilih dalam Javascript .
var SoldCounty = $find('');.
var sCounty = Daerah Terjual. dapatkan_nilai();
Kemudian saya bisa melakukan sesuatu dengan nilainya

Bagaimana cara memeriksa apakah opsi pilih tidak dipilih dalam JavaScript?

selectedIndex == -1 mengembalikan nilai true maka tidak ada opsi yang dipilih.

Bagaimana saya tahu opsi mana yang dipilih di dropdown?

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

Postingan terbaru

LIHAT SEMUA