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

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].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
6________94__95_______

 

<<!DOCTYPE html>4>

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

>2<>4 >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________93__1_______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

Javascript periksa apakah kotak kombo dipilih

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>

    <________98__95_______

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

 

<<!DOCTYPE html>4>

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

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

Javascript periksa apakah kotak kombo dipilih

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('<%= ddlSoldCounty. ClientID %>');.
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