ฉันสงสัยว่าทุกคนโพสต์เกี่ยวกับvalueและtextตัวเลือกที่จะได้รับจาก<option>และไม่มีใครแนะนำlabelและไม่มีใครแนะนำ
ดังนั้นฉันขอแนะนำ labelด้วยเช่นกันเนื่องจากเบราว์เซอร์ทั้งหมดรองรับ
ที่จะได้รับvalue(เช่นเดียวกับที่คนอื่นแนะนำ)
function test(a) {
var x = a.options[a.selectedIndex].value;
alert(x);
}
ที่จะได้รับoption text(เช่นการสื่อสารหรือ - เลือก -)
function test(a) {
var x = a.options[a.selectedIndex].text;
alert(x);
}
หรือ (คำแนะนำใหม่)
function test(a) {
var x = a.options[a.selectedIndex].label;
alert(x);
}
HTML
<select onchange="test(this)" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
<option value="2" label=‘newText’>Communication</option>
</select>
หมายเหตุ: ใน HTML ด้านบนสำหรับoptionค่า 2 labelจะส่งคืนnewTextแทนการสื่อสาร
นอกจากนี้
หมายเหตุ: ไม่สามารถตั้งค่าคุณสมบัติป้ายกำกับใน Firefox (ส่งคืนเท่านั้น)