如何使用 Javascript 进行乘法/求和 [英] How to Multiply / Sum with Javascript
本文介绍了如何使用 Javascript 进行乘法/求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
https://jsbin.com/wujusajowa/1/edit?html,js,输出
我可以总结选项的数量.喜欢 (5+5+5=15)
I can sum the numbers of options. Like (5+5+5=15)
但我不知道如何将输入乘以选择的总和.
But I don't know a way to multiply the input with the sum of selects.
例如,我应该怎样做 6 x (5+5+5) 并得到 90 ?
For example, What should I do to do 6 x (5+5+5) and get 90 ?
推荐答案
使用,定义一个全局变量来存储
的值;
;将 change
事件附加到 元素以更新全局变量;如果定义了变量,则在
元素的
change
事件中使用变量,否则使用 1
作为乘数
Use <input type="number">
, define a global variable to store value of <input>
; attach change
event to <input>
element to update global variable; use variable at change
event of <select>
element if variable is defined, else use 1
as multiplier
var input = 0;
$('select').change(function(){
var sum = 0;
$('select :selected').each(function() {
sum += Number($(this).val());
});
$("#toplam").html(sum * (input || 1));
}).change();
$("#miktar").on("change", function() {
input = this.valueAsNumber;
});
jsbin https://jsbin.com/cujohisahi/1/edit?html,js,输出
这篇关于如何使用 Javascript 进行乘法/求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文