[英]JavaScript: Add selected item to list on button click
I want to be able to add an item to a list (copy its name and its consumption) whenever I click the button "Anadir". 我希望每当单击按钮“ Anadir”时就可以将一个项目添加到列表中(复制其名称和使用情况)。 I have the following HTML:
我有以下HTML:
<form name="calcEquipo">
<table>
<tr>
<td align="center" bgcolor="#F4F4F4"><select name="aparato" id="aparato" onchange="copiaValor();" size="1">
<option value="">Seleccione una opción</option>
<option value="18.22" name="Aire inverter">Aire inverter (12,000 btu)</option>
<option value="22.97" name="Aire mini split">Aire mini split (12,000 btu)</option>
<option value="2.10" name="Abanico de techo">Abanico de techo</option>
<option value="0.48" name="Reproductor Blu-Ray">Reproductor Blu-Ray</option>
<option value="0.14" name="Mini componente">Mini componente</option>
Many other options... 许多其他选择...
<tr>
<td colspan="2" align="center" bgcolor="#25AE77"><h3>El consumo mensual del equipo será de:</h3>
</td>
</tr>
<tr>
<td colspan="2" align="center" bgcolor="#D4D4D4"><input type="text" name="consumototalkwh" id="consumototalkwh" size="12" readonly>
</td>
</tr>
<tr>
<td colspan="2" align="center" bgcolor="#F4F4F4"><input type="button" value="Añadir" onclick="anadirEquipo();" style="width: 165px; height: 50px; font-size: 16px;"/></td>
</td>
</tr>
<tr><td colspan="2" align="center" bgcolor="#25AE77">Equipos añadidos</td>
</tr>
<tr>
<td align="center" bgcolor="#F4F4F4"><div id="nameEquip"></div></td>
<td align="center" bgcolor="#F4F4F4"><div id="consumoEquip"></div></td>
</tr>
<tr>
<td colspan="2" align="center" bgcolor="#F4F4F4"><input type="button" value="Retirar" onclick="retirarEquipo();" style="width: 100px; height: 30px; font-size: 10px;"/>
</td>
</tr>
</table>
</form>
There is more code in HTML but its not relevant. HTML中有更多代码,但不相关。 All I want to do is to add the selected "aparato" option name and its "consumototalkwh" into the divs "nameEquip" and "consumoEquip", respectively, everytime I click the "Anadir" button.
我要做的就是每次我单击“ Anadir”按钮时,将选定的“ aparato”选项名称及其“ consumototalkwh”分别添加到divs“ nameEquip”和“ consumoEquip”中。
Can someone help me with this? 有人可以帮我弄这个吗? Here is my failed attempt on JS:
这是我在JS上失败的尝试:
function anadirEquipo () {
var dropdown = document.getElementById('aparato')
var optionName = document.getElementById('aparato').options[dropdown.selectedIndex].getAttribute('name')
var kwh=document.getElementById("consumototalkwh").value;
var container1=document.getElementById("nameEquip");
var container2=document.getElementById("consumoEquip");
document.getElementById("nameEquip").innerHTML=optionName;
document.getElementById("consumoEquip").innerHTML=kwh;
}
Like this, it copies correctly the first selected value, but if I change the selection and click again on "Anadir", my previous choice will be overwritten by the new one. 这样,它可以正确复制第一个选定的值,但是如果我更改选择并再次单击“ Anadir”,则我先前的选择将被新的覆盖。 How can I add without losing the previous selections?
如何添加而不丢失先前的选择?
You are not getting the drop down option name correctly... 您没有正确获得下拉选项名称...
var dropdown = document.getElementById('aparato');
var optionName = dropdown.options[dropdown.selectedIndex].getAttribute('name');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.