How to conduct arithmetic operations in JQuery?

var price = $('#addprice').val(); var pass = $('#pass').val(); var total = $('#totalprice').attr('value') var left = $('#leftquota').attr('value') var balance = $('#balance').attr('value') var tprice = total + price; // total price var bprice = balance + price; // balance price var unitprice = bprice / left; // unit price $('#totalprice').text(tprice); $('#balance').text(bprice); $('#unitprice').text(unitprice); JQuery just treats total, left, balance, tprice, bprice, unitprice,etc. as strings, but actually there are decimals rather than strings. When I apply parseInt() to them, they all become integers, which are not what I want. How to conduct arithmetic operations? The operands are decimals. I use parseFloat(); but it is the same. The operation of var tprice=total+price; just literally conjugates two decimals(strings) together. $('#add_price').click(function(){ var price=$('#addprice').val(); var pass=$('#pass').val(); var total=$('#totalprice').attr('value') var left=$('#leftquota').attr('value') var balance=$('#balance').attr('value') total=parseFloat(total); left=parseFloat(left); balance=parseFloat(balance); var tprice=total+price; var bprice=balance+price; var unitprice=bprice/left; $('#totalprice').text(tprice); $('#balance').text(bprice); $('#unitprice').text(unitprice);
It isn't jQuery treating them as strings, it is javascript treating them as strings (as they are strings). jQuery only gets the values. Javascript does the arimethics, jQuery is just a library on top of javascript.

以上就是How to conduct arithmetic operations in JQuery?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准