jQuery中的简单数学 [英] Simple math in jquery

查看:69
本文介绍了jQuery中的简单数学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取一个选择表单值,并在jquery中将其乘以50.在每次乘以50之前,我需要在选择菜单返回的数量上加上1.令人反感的代码如下所示.

I am reading a select form value and multiplying it by 50 in jquery. I need to add a value of 1 to the qty that is returned by the select menu every time before multiplying by 50. How would I do that? The offending code looks like this.

 $('#selectform').val() *50);

如果我使用

 $('#selectform').val() +1 *50);

结果不正确.

推荐答案

应使用括号.

 ($('#selectform').val()*1 + 1) *50;

您当前的表达式解释为:

Your current expression is interpreted as:

 var something = $('#selectform').val();
 var another   = 1 * 50;
 var result    = something + another

.val()之后的*1用于将字符串值转换为数字.如果省略,则表达式将解释为:

The *1 after .val() is used to convert the string value to a number. If it's omitted, the expression will be interpreted as:

var something = $('#selectform').val() + "1";  //String operation
var result    = something * 50;    // something is converted to a number, and
                                   //    multiplied by 50

这篇关于jQuery中的简单数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆