حدد إضافة طريقة ()
مثال
أضف خيار "كيوي" في نهاية القائمة المنسدلة:
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option);
التعريف والاستخدام
تستخدم طريقة add () لإضافة خيار إلى القائمة المنسدلة.
نصيحة: لإزالة خيار من القائمة المنسدلة ، استخدم طريقة الإزالة () .
دعم المتصفح
Method | |||||
---|---|---|---|---|---|
add() | Yes | Yes | Yes | Yes | Yes |
بناء الجملة
selectObject.add(option, index)
قيمه المعامل
Parameter | Description |
---|---|
option | Required. Specifies the option to add. Must be an option or optgroup element |
index | Optional. An integer that specifies the index position for where the new option element should be inserted. Index starts at 0. If no index is specified, the new option will be inserted at the end of the list |
تفاصيل تقنية
قيمة الإرجاع: | لا قيمة مردودة |
---|
مزيد من الأمثلة
مثال
أضف خيار "كيوي" في بداية القائمة المنسدلة:
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option, x[0]);
مثال
أضف خيار "كيوي" في موضع الفهرس "2" من القائمة المنسدلة:
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option, x[2]);
مثال
أضف خيارًا قبل الخيار المحدد في القائمة المنسدلة:
var x = document.getElementById("mySelect");
if (x.selectedIndex >= 0) {
var option = document.createElement("option");
option.text = "Kiwi";
var sel = x.options[x.selectedIndex];
x.add(option, sel);
}
❮ حدد الكائن