javascript – jQuery添加字符串而不是连接

前端之家收集整理的这篇文章主要介绍了javascript – jQuery添加字符串而不是连接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在从选择列表中添加值(或减去),我似乎无法使它不连接字符串.

我的选择清单是:

@H_403_7@
  • 我的javascript是:

    基本上我正在尝试让currentAccess将当前值作为整数并根据需要进行加减.

    汤姆

  • 最佳答案
    使用整数而不是字符串.

    >将currentAccess初始化为零.
    > – =运算符给模糊性留下了很少的位置,但是
     =运算符可以表示“连接字符串”或“添加数字”.
    >使其更明显,convert the variable to a number,例如使用* 1(次1)

    更新的代码

    @H_403_7@$("select").change(function () { var currentAccess = 0; // Instead of "0" var currentText = $(":selected",this).text(); var value = $(this).val() * 1; alert(currentAccess); if (currentText == "Off") { currentAccess -= value; } if (value != 0) { if (currentText == "On") { currentAccess += value; } } $.cookie("accessReqs",{ expires: 24 }); alert(currentAccess); });

    猜你在找的jQuery相关文章