为什么改变我的表单输入值不出现在Javascript中
Why is the change to my form input value not appearing in Javascript?
我正在编写一个脚本,用于计算客户订购啤酒箱的成本。有一个动态生成啤酒选项的选择菜单,客户必须从中选择,其中包括项目名称和价格。用户选择他们的数量,点击"添加项目"按钮。当单击此按钮时,商品的名称和价格应添加到"商品描述"列下的第一个可用输入中。
问题是没有出现对"descriptionInput0"输入的更改。当我将"firstAvailable"变量记录到控制台时,更改会出现在控制台,但不会出现在浏览器中。我不太清楚发生了什么。什么好主意吗?谢谢你的宝贵时间。
这是一个jsFiddle和代码。
window.onload = calculator();
function calculator() {
var descriptions = new Array("Shamrock Ale", "Lucky Pilsner", "Irish Wheat", "Irish Malt", "Shamrock Rye");
var prices = new Array(24.99, 27.99, 27.99, 32.99, 35.99);
populateSelectMenu(descriptions, prices);
var descriptionMenu = document.getElementById("descriptionMenu");
addButton = document.getElementById("addItem");
addButton.onclick = function () {
firstAvailable = detectAvailable();
firstAvailable = document.getElementsByName("descriptionInput" + firstAvailable);
console.log(firstAvailable);
firstAvailable.value = descriptionMenu.options[descriptionMenu.selectedIndex].text;
}
}
function populateSelectMenu(descriptions, prices) {
var descriptionMenu = document.getElementById("descriptionMenu");
for (var i = 0; i < descriptions.length; i++) {
option = document.createElement("option");
option.value = i;
option.innerHTML = descriptions[i] + ": $" + prices[i];
descriptionMenu.appendChild(option);
}
}
function detectAvailable() {
var descriptionInputs = document.querySelectorAll("input[name^='descriptionInput']");
inputs: for (var i = 0; i < descriptionInputs.length; i++) {
if (descriptionInputs[i].value != '') {
continue inputs;
} else {
var firstAvailable = i;
break inputs;
}
}
return firstAvailable;
}
<form name="calculator" id="calculator">
<div id="userInput">
<select name="descriptionSelect" id="descriptionMenu"></select>
<label>Quantity</label>
<input type="number" name="quantity" min="1" max="10" size="2" value="1">
<input type="button" value="Add to List" id="addItem">
</div>
<table align="center" cellspacing="1" cellpadding="5" width="40%" border="1">
<thead>
<tr>
<th>Item Description</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="descriptionInput0" value="" size="30">
</td>
<td>
<input type="text" name="uPriceInput0" value="" size="5">
</td>
<td>
<input type="text" name="quantityInput0" value="" size="5">
</td>
<td>
<input type="text" name="tPriceInput0" value="" size="5">
</td>
</tr>
<tr>
<td>
<input type="text" name="descriptionInput1" value="" size="30">
</td>
<td>
<input type="text" name="uPriceInput1" value="" size="5">
</td>
<td>
<input type="text" name="quantityInput1" value="" size="5">
</td>
<td>
<input type="text" name="tPriceInput1" value="" size="5">
</td>
</tr>
<tr>
<td>
<input type="text" name="descriptionInput2" value="" size="30">
</td>
<td>
<input type="text" name="uPriceInput2" value="" size="5">
</td>
<td>
<input type="text" name="quantityInput2" value="" size="5">
</td>
<td>
<input type="text" name="tPriceInput2" value="" size="5">
</td>
</tr>
<tr>
<td>
<input type="text" name="descriptionInput3" value="" size="30">
</td>
<td>
<input type="text" name="uPriceInput3" value="" size="5">
</td>
<td>
<input type="text" name="quantityInput3" value="" size="5">
</td>
<td>
<input type="text" name="tPriceInput3" value="" size="5">
</td>
</tr>
<tr>
<td>
<input type="text" name="descriptionInput4" value="" size="30">
</td>
<td>
<input type="text" name="uPriceInput4" value="" size="5">
</td>
<td>
<input type="text" name="quantityInput4" value="" size="5">
</td>
<td>
<input type="text" name="tPriceInput4" value="" size="5">
</td>
</tr>
<tr>
<td colspan="3" align="right">Total Price:</td>
<td>
<input type="text" name="totalPrice" size="5" disabled>
</td>
</tr>
</tbody>
</table>
</form>
firstAvailable[0].value = descriptionMenu.options[descriptionMenu.selectedIndex].text;
firstAvailable
是NodeList[1]
一个数组,所以你需要指定索引来获得NodeElement
。
只是一个建议。
function detectAvailable() {
var descriptionInputs = document.querySelectorAll("input[name^='descriptionInput']");
for (var i = 0; i < descriptionInputs.length; i++) {
if (descriptionInputs[i].value == '') {
return descriptionInputs[i];
}
}
}
和
addButton.onclick = function () {
var firstAvailable = detectAvailable();
firstAvailable.value = descriptionMenu.options[descriptionMenu.selectedIndex].text;
}
相关文章:
- JavaScript改变了双方的显示风格
- Javascript改变位置赢得'不适用于Chrome或IE
- Hov 我用 javascript 改变风格,只影响第一个 ul
- Javascript:改变内部HTML与显示
- 你能用Javascript改变一个表的背景属性吗
- 当javascript改变两个标签的宽度时,它们会自动改变位置
- Javascript-改变图像的宽度
- JavaScript改变DIV背景使用ID
- jQuery/Javascript:改变一个锚定标签的onclick
- 用JavaScript改变HTML中的Tex字符串
- 用javascript改变css样式
- 在鼠标悬停时,使用javaScript改变RadRotator的RotatorType属性
- 只有Javascript -改变图像悬停在隐藏的单选按钮
- 正则表达式javascript改变url中的参数值
- 如何通过javascript改变css伪元素
- 如何用javascript改变android webView的字体
- 通过JavaScript改变DevExpress GridView在SetFocusedRowIndex(-1)之后的行
- Javascript改变图像/背景图像不工作
- 使用JavaScript改变整个页面的前景和背景颜色
- 使用JavaScript改变SVG路径的颜色(JS颜色库)