【问题标题】:Unexpected result from Javascript functionJavascript函数的意外结果
【发布时间】:2012-09-10 19:28:44
【问题描述】:

我是 JavaScript 新手,但如果有人能告诉我我缺少什么,我将不胜感激。

基本上,我正在尝试从两个输入中测试较大的值。这是我到目前为止所做的:

$('#than_stock_submit').click(function() {
    var pur_rate = $('#pur_rate input').val(),
        sell_rate = $('#sell_rate input').val(),
        msg_div = $('#sell_rate .msg');

    if(greater_than(sell_rate, pur_rate, msg_div)==false){return false}
});

function greater_than(a, b, msg_div){
    msg_div.show().html( '' );
    if(a > b){
        msg_div.show().html( '<p class="success">Sell Rate is good</p>' );
        return true;
    } else {
        msg_div.show().html( '<p class="error">Sell Rate should be increased</p>' );
        return false;
    }
}

我已经检查了几个值。当我用小于 1000 的值进行测试并且两个值类似 b=500 和 a=5000 或 b=100 和 a=1000 时,它就可以工作了。其他值无效。

其他测试值为:

  1. a=751、b=750 和结果=true
  2. a=0751、b=750 和结果=false
  3. a=551, b=750 和结果=false
  4. a=1051, b=750 和结果=false
  5. a=7500、b=750 和结果=true
  6. a=6000, b=600 和结果=true

我还检查了控制台,例如:console.log(a + b);

控制台窗口的结果类似于 1000750(当值类似于 a=1000 & b=750)或 0752750(当值类似于 a=0752 & b=750)。

谢谢。

【问题讨论】:

    标签: javascript jquery variables conditional-statements


    【解决方案1】:

    这是一个更强大的解决方案(您正在做的是字符串比较而不是数字比较)。

    function greater_than(a,b) {
      // first, convert both passed values to numbers
      // (or at least try)
      var nA = new Number(a),
          nB = new Number(b);
    
      // check if they were converted successfully.
      // isNaN = is Not a Number (invalid input)
      if (!isNan(nA) && !isNaN(nB)) {
        // now go ahead and perform the check
        msg_div.empty().show();
        if (nA > nB) {
          $('<p>',{'class':'success'})
            .text('Sell Rate is good')
            .appendTo(msg_div);
          return true;
        } else {
          $('<p>',{'class':'error'})
            .text('Sell Rate should be increased')
            .appendTo(msg_div);
        }
      }
      // In case you wanted to handle showing an error for
      // invalid input, you can uncomment the following lines
      // and take the necessary action(s)
      else{
        /* one of them was not a number */
      }
      return false;
    }
    

    请注意,我使用 jQuery 构建了您添加的 &lt;p&gt;。我还使用了.empty() 而不是.html('')

    还有一些文档:

    【讨论】:

    • 太棒了!非常感谢。你能告诉我你为什么使用 $('

      ',{'class':'success'}) .text('Sell Rate is good') .appendTo(msg_div);而不是 msg_div.show().html( '

      Sell Rate is good

      ' );和 .empty() 而不是分配 .html('')
    • 使用提供的方法构建 DOM 比提供显式 HTML 更安全。
    【解决方案2】:

    读取输入值返回字符串。因此,如果您将字符串与字符串进行比较,则它是 ASCII 比较,而不是数字比较。请使用parseInt(value, 10); 永远不要忘记基数! ;)

    【讨论】:

      【解决方案3】:

      您应该在比较之前将字符串转换为数字(使用.val() 时它们会变成字符串)。使用parseIntparseFloat

      function greater_than(a, b, msg_div){
          a = parseInt(a, 10);
          b = parseInt(b, 10);
          // etc
      

      【讨论】:

      • 并链接到parseFloatparseIntparseInt 表示整数,parseFloat 表示可能包含十进制数字的数字)
      • 但是你能告诉我,10 是什么?
      • 第二个参数是基数。 JavaScript 有一些 C 遗留行为,如果您传递 f.ex parseInt(03) 它可以在某些 ECMA 版本中将其解释为 八进制值。传递 10 作为第二个参数确保十进制解释。
      【解决方案4】:

      您正在比较字符串并且"1000"&gt;"99" 为假。

      解决方案是首先使用parseIntparseFloat 解析您的数字:

       var pur_rate = parseFloat($('#pur_rate input').val());
      

       var pur_rate = parseInt($('#pur_rate input').val(), 10);
      

      【讨论】:

        猜你喜欢
        • 2021-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多