我正在读取一个选择表单值并在 jquery 中将其乘以 50。我需要在每次乘以 50 之前为选择菜单返回的数量添加 1 的值。我该怎么做?违规代码如下所示。
$('#selectform').val() *50);
如果我用
$('#selectform').val() +1 *50);
结果不正确。
最佳答案
应该使用括号。
($('#selectform').val()*1 + 1) *50;
您当前的表达式被解释为:
var something = $('#selectform').val();
var another = 1 * 50;
var result = something + another
.val()
后的*1
用于将字符串值转换为数字。如果省略,表达式将被解释为:
var something = $('#selectform').val() + "1"; //String operation
var result = something * 50; // something is converted to a number, and
// multiplied by 50
关于javascript - jquery中的简单数学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7971292/