[英]JavaScript - Append option element to a select element not working
I'm trying to append an option element to a select element using JavaScript but I can't get it to work. 我正在尝试使用JavaScript将option元素附加到select元素,但无法正常工作。 I broke the code into pieces and called console.log()
everywhere I imagined a possible mistake, but everything seems to be working fine, except the appendChild method. 我将代码分解成碎片,并在所有我想到的错误地方都调用了console.log()
,但是除了appendChild方法之外,其他一切似乎都可以正常工作。
I have also tried with: select.add(new option('value1', 'value2')
but it didn't work. 我也尝试过: select.add(new option('value1', 'value2')
但是没有用。
The Javascript Code: JavaScript代码:
var divTo = document.getElementById('selectProduct');
console.log(divTo);
if (whereTo == 'selectProduct'){
console.log('inside if loop');
for (i = 0 ; i < 2 ; i++){
console.log('inside for loop')
console.log('products[i] = ' +products[i]);
var addedOption = new Option();
addedOption.label = products[i];
console.log("addedOption label = "+addedOption.label);
divTo.appendChild(addedOption);
console.log("divTo constructor = "+divTo.constructor.name);
console.log("addedOptionConstructor = "+addedOption.constructor.name);
// also tried: divTo.options.add(new Option("valor1", "valor2")); instead of appendChild
// also tried: addedOption.text instead of addedOption.label
}
}
The console logs: 控制台日志:
inside if loop
inside for loop
products[i] = Armada Argentina
addedOption label = Armada Argentina
divTo constructor = HTMLSelectElement
addedOptionConstructor = HTMLOptionElement
<select class="selectfield" id="selectProduct" name="product"></select>
Could someone help me out?. 有人可以帮我吗?
PS: I'm really new to programming. PS:我真的是编程新手。 I'm sorry if the mistake is evident. 如果错误很明显,我感到抱歉。
Use this, 用这个,
var divTo = document.getElementById('selectProduct');
if (whereTo == 'selectProduct') {
console.log('inside if loop');
for (i = 0 ; i < 2 ; i++) {
var addedOption = document.createElement("option");
addedOption.text = products[i];
divTo.add(addedOption);
}
}
createSelect()
creates a select and options by passing 2 equal length arrays and a target element to append to. createSelect()
通过传递2个等长数组和要附加到的目标元素来创建选择和选项。
Signature 签名
createSelect(textArray, valueArray, target)
Parameters 参量
- textArray : An array of strings--each string set to an option's content-- length must be equal to the length of valueArray . textArray :字符串数组(每个字符串设置为选项的内容)的长度必须等于valueArray的长度 。
- valueArray : An array of strings--each string set to an option's value-- length must be equal to the length of textArray . valueArray :字符串数组(每个字符串设置为选项的值)的长度必须等于textArray的长度 。
- target : A string--syntax same as CSS and jQuery selector. target :一个字符串,语法与CSS和jQuery选择器相同。
var txt = ['ONE', 'TWO', 'THREE', 'FOUR', 'FIVE']; var val = ['1', '2', '3', '4', '5']; function createSelect(textArray, valueArray, target) { var tgt = document.querySelector(target); var sel = document.createElement('select'); var qty = textArray.length; for (let i = 0; i < qty; i++) { var opt = document.createElement('option'); opt.textContent = textArray[i]; opt.value = valueArray[i]; sel.appendChild(opt); } tgt.appendChild(sel); } createSelect(txt, val, "body");
For onload
, wrap it in another function: 对于onload
,将其包装在另一个函数中:
function init() { var txt = ['ONE', 'TWO', 'THREE', 'FOUR', 'FIVE']; var val = [1, 2, 3, 4, 5]; function createSelect(textArray, valueArray, target) { var tgt = document.querySelector(target); var sel = document.createElement('select'); var qty = textArray.length; for (let i = 0; i < qty; i++) { var opt = document.createElement('option'); opt.textContent = textArray[i]; opt.value = valueArray[i]; sel.appendChild(opt); } tgt.appendChild(sel); } createSelect(txt, val, "body"); } window.onload = init;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.