问题描述
我有一个js函数,它读取JSON产品列表,然后为每个产品在选项菜单中创建一个select。效果很好,但是现在我需要实现两个字段,即价格和名称,以便将新产品手动添加到上一个下拉菜单中。
我正在尝试使用已经创建选择选项的相同功能,但是本应获取新产品的事件侦听器仅在加载页面时触发,创建空白选项,然后当我单击按钮时它不起作用任何东西。而且我不知道该试试什么。
这是html:
<select id='productList'></select>
<input id="unidades" type="number" value="0" placeholder="Unidades">
<!--<img src="data/icons/add.svg" alt="Add" id="calcula" class="icon">-->
<button id="addToCart">Añadir al carrito</button>
<!--Div que contiene el formulario para añadir productos al inventario de productos-->
<div>
<input id="newProductName" type="text" placeholder="Nombre del producto">
<input id="newProductPrice" type="number" placeholder="Precio">
<button id="addToSelectMenu">Añadir al inventario</button>
</div>
</div>
这是监听ubtton并向菜单添加新选项的JS代码:
const addSelectOption = (name,price) => {
console.log("funcion añador opc al select");
const select = document.createElement('option'); // Elemento a crear de tipo option
select.innerHTML = name; // Texto de entre los tags <option>
select.setAttribute("value",price) // Asignamos como atrinuto el precio
select.setAttribute("name",name);
optionList.appendChild(select); // Añadimos el elemento al select
return;
}
operation.addEventListener('click',addProductCart); // Escuchamos el boton calcula prar mostrar el precio total
addProductToInventori.addEventListener('click',addSelectOption(newProductName.value,newProductPrice.value));
// This function reads the JSON file and send the product name and price to addSelectOption function
loadProducts();
我认为这可能是一个基本问题,但无法弄清楚如何解决。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)