Saya hanya menebak-nebak lagi karena tidak ada kode. Tetapi semua tahun memiliki nilai "Semua" jadi jika itu adalah nilai yang dikirimkan maka Anda tidak boleh menambahkan apa pun ke kueri untuk memfilter posting berdasarkan tahun
Sejauh menampilkan semua posting, ini dilakukan dengan menyetel argumen kueri posts_per_page ke -1
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.valueContoh. Contoh ini menjelaskan properti nilai yang dapat ditemukan untuk elemen yang dipilih
HTML
<!DOCTYPE html>
<head>
<________100__97_______
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>
<<!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>
________97______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>
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>
<________100__97_______
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>
<<!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>
________97______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].value_1<!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