Javascript 铜到银、金的转化率为20
我是新来的,所以。。。我有4个输入类型=“数字”字段,分别是铜、银、金和白金。当玩家将数字添加到任何数字框并单击“添加”时该值需要转到相应的总数,数字框需要恢复为零。当总数超过1000时,需要从该总数中减去-1000,并将1添加到下一个总数字段,以此类推。例如:玩家输入105500铜币,然后单击“添加”按钮它需要更新所有总计:铜=500+当前值,银=5+当前值,金=1+当前值,板=0+当前值。我还需要它能够减去和更新所有总计。我已经添加了一个我找到的解决方案,我相信它会起作用,但我不确定如何使其与当前字段一起工作。或者如果有完成这项任务的更好方法我洗耳恭听 下面是我开始的Javascript 铜到银、金的转化率为20,javascript,html,Javascript,Html,我是新来的,所以。。。我有4个输入类型=“数字”字段,分别是铜、银、金和白金。当玩家将数字添加到任何数字框并单击“添加”时该值需要转到相应的总数,数字框需要恢复为零。当总数超过1000时,需要从该总数中减去-1000,并将1添加到下一个总数字段,以此类推。例如:玩家输入105500铜币,然后单击“添加”按钮它需要更新所有总计:铜=500+当前值,银=5+当前值,金=1+当前值,板=0+当前值。我还需要它能够减去和更新所有总计。我已经添加了一个我找到的解决方案,我相信它会起作用,但我不确定如何使其
<html>
<body>
<div class = "sheet-currency">
<label> Copper </label>
<input type = "numbers" name = "attr_copper"></input>
<span name = "coppertotal">0</span>
</div>
<input type = "button" value = "Add" onclick = “addMoney”></input>
<input type = "button" value = "Sub" onclick = “addMoney” ></input>
<br></br>
<div>
<label> Silver </label>
<input type = "numbers" name = "attr_silver"></input>
<span name = "silvertotal">0</span>
</div>
<input type = "button" value = "Add" onclick = “addMoney” ></input>
<input type = "button" value = "Sub" onclick = “addMoney” ></input>
<br></br>
<div>
<label> Gold </label>
<input type = "numbers" name = "attr_gold"></input>
<span name = "goldtotal">0</span>
</div>
<input type = "button" value = "Add" onclick = “addMoney” ></input>
<input type = "button" value = "Sub" onclick = “addMoney” ></input>
<br></br>
<div>
<label> Plat <label>
<input type = "numbers" name = "attr_plat"></input>
<span name = "plattotal">0</span>
</div>
<input type = "button" value = "Add" onclick = “addMoney” ></input>
<input type = "button" value = "Sub" onclick = “addMoney” ></input>
<br></br>
<script>
var copper = 0;
var silver = 0;
var gold = 0;
var plat = 0;
function addMoney(type, amount){
if(type=='copper'){
copper += amount;
while(copper >= 1000){
copper -= 1000;
silver++;
}
while(silver >= 1000){
silver -= 1000;
gold++;
}
} else if(type=='silver'){
silver += amount;
while(silver >= 1000){
silver -= 1000;
gold++;
}
} else if(type=='gold'){
gold += amount;
while(gold >= 1000){
gold -= 1000;
plat++;
}
} else if(type=='plat'){
plat += amount;
} else {
return false;
}
return true;
}
</script>
</body>
</html>
铜
0
银
0
金
0
小块地
0
var铜=0;
var银=0;
var-gold=0;
var plat=0;
函数addMoney(类型、金额){
如果(类型==“铜”){
铜+=数量;
而(铜>=1000){
铜-=1000;
银++;
}
而(银>=1000){
银-=1000;
黄金++;
}
}else if(类型=='silver'){
银+=数量;
而(银>=1000){
银-=1000;
黄金++;
}
}else if(类型=='gold'){
黄金+=金额;
而(黄金>=1000){
黄金-=1000;
plat++;
}
}else if(类型=='plat'){
plat+=金额;
}否则{
返回false;
}
返回true;
}
对脚本和html进行少量更改即可获得结果。1.删除所有
没有此类标签2.为
addMoney
功能定义参数3.使用JavaScript的getElementById方法获取textbox的值,并在span内设置文本。 4.您需要定义
子货币
类似地使用减法逻辑,现在就是您自己了;)
铜
0
银
0
金
0
小块地
0
var铜=0;
var银=0;
var-gold=0;
var plat=0;
函数addMoney(类型、金额文本框){
var amount=Number(document.getElementById(amountTextBox).value);
如果(isNaN(金额))
{
回来
}
如果(类型==“铜”){
铜+=数量;
而(铜>=1000){
铜-=1000;
银++;
}
而(银>=1000){
银-=1000;
黄金++;
}
}
else if(类型=='silver'){
银+=数量;
而(银>=1000){
银-=1000;
黄金++;
}
}
else if(类型=='gold'){
黄金+=金额;
而(黄金>=1000){
黄金-=1000;
plat++;
}
}
else if(类型=='plat'){
plat+=金额;
}
否则{
返回false;
}
document.getElementById(“coppertotal”).textContent=copper;
document.getElementById(“silvertotal”).textContent=silver;
document.getElementById(“goldtotal”).textContent=gold;
document.getElementById(“plattotal”).textContent=plat;
document.getElementById(amountTextBox).value=0;
返回true;
}
非常感谢!我将尝试将此逻辑应用于减法部分。