单击Cost计数输入类型号(增量和递减)应以“总成本”显示值。
点击数量输入类型数(增量和减少)应从总成本中取值并计算,在总成本时显示值。
<input class="form-control" type="number" id="wordcount" onchange="calTotal();" name="wordcount" value="" min="1" max="100000000000" id="wordcount" >
<input class="form-control" type="number" id="quantity" onchange="calTotal();" name="quantity" value="" min="1" max="100000000000">
在这里显示结果:
<input type="number" readonly="readonly" min="1n" name="total" id="running-summary-total" value="0.00">
进行计算的Javascript:
<script>
function calTotal(){
var z = document.getElementById("wordcount").value;
console.log(z)
if (document.getElementById("wordcount").value) {
var wc = z * 0.098;
document.getElementById("running-summary-total").value = +wc;
}
if (document.getElementById("quantity").value) {
var r = document.getElementById("running-summary-total").value
var wc = r * 2;
document.getElementById("running-summary-total").value = +wc;
}
}
</script>
我对wordcount
函数没有异议,但是当我减少document.getElementById("quantity").value
中的值时,它是在递增,而不是减少2。
发布于 2020-06-16 02:29:23
根据这些评论并提供了解释。我想这就是你要找的:
function calTotal(){
let wordcount = $("#wordcount").val();//get wordcount value
let quantity = $("#quantity").val();//get quantity value
if(wordcount){//if wordcount has value
var wc = wordcount * 0.098;
$("#running-summary-total").val(wc);
}
if(quantity && quantity < 0){//if quantity has value and its smaller then 0 (negative value)
let oldtotal = $("#running-summary-total").val();//getting the old total value
let calc = Math.abs(quantity) * 2;//added Math.abs to turn negative number into positive, and mulitply by 2
let newtotal = oldtotal / calc;// here we divide the old total by the quatity times 2
$("#running-summary-total").val(newtotal);
}
if(quantity && quantity > 0){//if quantity has value and its bigger then 0 (positive value)
let oldtotal = $("#running-summary-total").val();//getting the old total value
let calc = quantity * 2;//here we multiply quantity by 2
let newtotal = oldtotal * calc;// here we multiply the old total by the quantity times 2
$("#running-summary-total").val(newtotal);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="form-control" type="number" id="wordcount" onchange="calTotal();" name="wordcount" value="" min="1" max="100000000000">
<input class="form-control" type="number" id="quantity" onchange="calTotal();" name="quantity" value="" min="" max="100000000000">
<input type="number" readonly="readonly" min="1n" name="total" id="running-summary-total" value="0.00">
我已经将它重写为jquery代码(我的普通javascript不是很好)
编辑增加了对负值的支持,对正值的乘法(最初是除法)。
发布于 2020-06-16 02:02:27
您的代码目前正在接受总和的值,并将它们乘以2,而不考虑数量输入的值或字计数的值。
function calTotal(){
var wordCount = document.getElementById("wordcount").value;
let quantity = document.getElementById("quantity").value;
document.getElementById("running-summary-total").value = wordCount * quantity
}
https://stackoverflow.com/questions/62404981
复制相似问题