How to Multiply / Sum with Javascript [closed]

倾然丶 夕夏残阳落幕 提交于 2020-01-11 14:52:07

问题


https://jsbin.com/wujusajowa/1/edit?html,js,output

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.

For example, What should I do to do 6 x (5+5+5) and get 90 ?


回答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,output




回答2:


Here's a simple example:

var mySum = 6 * ( 5 + 5 + 5 );
document.getElementById('result').innerHTML = mySum;
<div id="result"></div>


来源:https://stackoverflow.com/questions/39714387/how-to-multiply-sum-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!