ရွေးချယ်စရာများ စုစည်းမှု ကို ရွေး ပါ။
ဥပမာ
တိကျသော drop-down list တစ်ခုတွင် ရွေးချယ်စရာ မည်မျှရှိသည်ကို ရှာဖွေပါ-
var x = document.getElementById("mySelect").options.length;
x ၏ရလဒ်သည် -
4
အောက်တွင် နောက်ထပ် "စမ်းသုံးကြည့်ပါ" ဥပမာများ။
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
ရွေးချယ်စရာများ စုစည်းမှုသည် drop-down list ရှိ <option> ဒြပ်စင်များအားလုံးကို စုစည်းမှုကို ပြန်ပေးသည်။
မှတ်ချက်- အရင်းအမြစ်ကုဒ်တွင် ပေါ်လာသည်နှင့်အမျှ စုစည်းမှုရှိ အစိတ်အပိုင်းများကို စီထားသည်။
Browser ပံ့ပိုးမှု
Collection | |||||
---|---|---|---|---|---|
options | Yes | Yes | Yes | Yes | Yes |
အထားအသို
selectObject.options
သတ္တိ
Property | Description |
---|---|
length | Returns the number of <option> elements in the collection. Note: This property is read-only |
selectedIndex | Sets or returns the index of the selected <option> element in the collection (starts at 0) |
နည်းလမ်းများ
Method | Description |
---|---|
[index] | Returns the <option> element from the collection with the specified index (starts at 0). Note: Returns null if the index number is out of range |
[add(option[,index])] | Adds an <option> element into the collection at the specified index. If no index is specified, it inserts the option at the end of the collection |
item(index) | Returns the <option> element from the collection with the specified index (starts at 0). Note: Returns null if the index number is out of range |
namedItem(id) | Returns the <option> element from the collection with the specified id. Note: Returns null if the id does not exist |
remove(index) | Removes the <option> element with the specified index from the collection |
နည်းပညာဆိုင်ရာအသေးစိတ်
DOM ဗားရှင်း- | Core Level 2 Document Object |
---|---|
ပြန်ပို့တန်ဖိုး- | <select> ဒြပ်စင်ရှိ <option> ဒြပ်စင်အားလုံးကိုကိုယ်စားပြုသော HTMLOptionsCollection Object တစ်ခု။ အရင်းအမြစ်ကုဒ်တွင် ပေါ်လာသည်နှင့်အမျှ စုစည်းမှုရှိ အစိတ်အပိုင်းများကို စီထားသည်။ |
နောက်ထပ် ဥပမာများ
ဥပမာ
[ အညွှန်း ]
drop-down list တွင် ပထမရွေးချယ်မှု (အညွှန်းကိန်း 0) ၏ စာသားကို ရယူပါ။
var x = document.getElementById("mySelect").options[0].text;
x ၏ရလဒ်သည် -
Apple
ဥပမာ
ပစ္စည်း( အညွှန်း )
drop-down list တွင် ပထမရွေးချယ်မှု (အညွှန်းကိန်း 0) ၏ စာသားကို ရယူပါ။
var x = document.getElementById("mySelect").options.item(0).text;
x ၏ရလဒ်သည် -
Apple
ဥပမာ
အမည်ရှိပစ္စည်း( id )
drop-down list တွင် id="orange" ဖြင့် ရွေးချယ်မှု၏စာသားကို ရယူပါ-
var x = document.getElementById("mySelect").options.namedItem("orange").text;
x ၏ရလဒ်သည် -
Orange
ဥပမာ
drop-down list တွင် အညွှန်းအနေအထား "1" တွင် "Kiwi" option ကို ထည့်ပါ-
var x = document.getElementById("mySelect");
var c = document.createElement("option");
c.text = "Kiwi";
x.options.add(c, 1);
ဥပမာ
drop-down list မှ အညွှန်း "1" ပါသည့် ရွေးစရာကို ဖယ်ရှားပါ-
var x = document.getElementById("mySelect");
x.options.remove(1);
ဥပမာ
drop-down list တစ်ခုရှိ ရွေးချယ်စရာအားလုံးကို လှည့်ပတ်ပြီး ရွေးချယ်မှုတစ်ခုစီ၏ စာသားကို ထုတ်ပေးသည်-
var x = document.getElementById("mySelect");
var txt = "";
var i;
for (i = 0; i < x.length; i++) {
txt = txt + x.options[i].text + "<br>";
}
txt ၏ရလဒ်သည် -
Apple
Orange
Pineapple
Banana
ဥပမာ
drop-down list တွင် ရွေးစရာတစ်ခုကို ရွေးပြီး id="demo" ဖြင့် ဒြပ်စင်တစ်ခုတွင် ရွေးချယ်ထားသော ရွေးချယ်မှု၏ စာသားကို ထုတ်ပေးသည်-
var x = document.getElementById("mySelect");
var i = x.selectedIndex;
document.getElementById("demo").innerHTML = x.options[i].text;
ရလဒ်မှာ-
Banana
ဥပမာ
အခြား drop-down list တွင်ရွေးချယ်ထားသောရွေးချယ်မှုပေါ်မူတည်၍ drop-down list မှရွေးချယ်စရာများကိုပြောင်းပါ-
var carsAndModels = {};
carsAndModels['VO'] = ['V70', 'XC60', 'XC90'];
carsAndModels['VW'] = ['Golf', 'Polo', 'Scirocco', 'Touareg'];
carsAndModels['BMW'] = ['M6', 'X5', 'Z3'];
function ChangeCarList() {
var carList = document.getElementById("car");
var modelList = document.getElementById("carmodel");
var selCar = carList.options[carList.selectedIndex].value;
while (modelList.options.length) {
modelList.remove(0);
}
var cars = carsAndModels[selCar];
if (cars) {
var i;
for (i = 0; i < cars.length; i++) {
var car = new Option(cars[i], i);
modelList.options.add(car);
}
}
}
❮ Object ကို ရွေးပါ။