【发布时间】:2019-06-27 09:21:11
【问题描述】:
我正在尝试获取花费了多少的输入值,选择选项中的一个值来解释它的花费,并将其列在底部。我的想法是每次提交输入时将值放在一个数组中并将其显示在底部(我在一个按钮中有多个功能)例如:
今天你度过了:
15 杂货
22 外出
7 其他:买一双袜子。
我尝试了几种不同的方法: 我想确认一下,以防用户想添加其他花钱方式。我试过用 .innerHTML 和 .value 进行调整
function listSpending(){
var repeat, spending= [];
while (repeat == true){spending.push(getElementById("money_spent").innerHTML,getElementById("whatOn").innerHTML )
repeat = confirm("Did you spend money on anything else?");
listTotalSpent.innerHTML= listOfSpending;`;
}
}
这不起作用,所以我尝试尝试 getElementByName。从我读到的内容来看,这应该会自动获取具有相同名称的 HTML 并将其放入一个数组中。这也没有显示。
function listSpending(){
listOfSpending= document.getElementByName("moneySpent").value;
listTotalSpent.innerHTML= listOfSpending;
}
HTML 和 JS:
function storeCurrent(){
currentCash= document.getElementById("current").value;
// check if the entered value is number
if (isNaN(Number(currentCash))) {
alert("Numbers only");
} else {
currentTotal.innerHTML = `Current total is:${currentCash}`;
}
}
function checkOnWhat(val) {
var element = document.getElementById('whatOn');
if (val == 'Other')
element.style.display = 'block';
else
element.style.display = 'none';
}
function deductFromTotal() {
var spent = document.getElementById("money_spent").value;
// check if the entered value is number
if (isNaN(Number(spent))) {
alert("Numbers only");
} else {
//currentCash = currentCash - spent;
currentCash -= spent;
currentTotal.innerHTML = `Current total is:${currentCash}`;
}
}
/*function listSpending(){
listOfSpending= document.getElementByName("moneySpent").value;
listTotalSpent.innerHTML= listOfSpending;
}*/
/*function listSpending(){
var repeat, spending= [];
while (repeat == true){spending.push(getElementById("money_spent").innerHTML,getElementById("whatOn").innerHTML )
repeat = confirm("Did you spend money on anything else?");
listTotalSpent.innerHTML= listOfSpending;`;
}
}*/
<input type="amount" size=25 Placeholder="How much is in your wallet?"
id="current">
<input type="button" value ="confirm" onClick="storeCurrent()">
<br>
<h2 id="currentTotal">Current total is:
</h2>
<input type="amount" size=25 Placeholder="How much did you spend today?"
id="money_spent" name="moneySpent" required>
<select name="moneySpent" onchange='checkOnWhat(this.value);' required>
<option> Groceries
<option>Going out
<option>Bills
<option>Other</select>
<input type="text" name="whatOn" id="whatOn" style='display:none;'
required />
<p>
<input type="button" value="confirm" onClick="deductFromTotal()" ;
"listSpending()">
<h3 name="totalSpent" id="totalSpent"> Today's Total Spending: </h3>
<div id="listTotalSpent"></div>
【问题讨论】:
-
我给你做了一个sn-p。请修复它,使其成为minimal reproducible example - 例如,您缺少
currentCash -
感谢您的 sn-p!我在 HTML 的顶部有当前现金,但我认为没有必要包含我遇到的这个问题。
-
请更新 sn-p 以正常工作
-
我已经更新了 sn-ps 以包含所有必要和需要的信息,以了解问题并解决问题
-
仍然没有currentCash
标签: javascript html