How to use comparison operator in Javascript for two input values

后端 未结 4 1499
梦如初夏
梦如初夏 2021-01-27 04:01

I want to compare the two input values, just try in javascript, but it\'s not working fine. I\'m using the following code

function check_closing()
{
var opening         


        
4条回答
  •  情歌与酒
    2021-01-27 04:36

    The values of input elements are always strings. To compare them as numbers, you must convert them to numbers. You can do that with:

    • parseInt if they're whole numbers and you want to specify the number base and stop parsing at the first non-numeric character

      Example:

      var opening = parseInt($('#opening').val(), 10);
      // This is the number base, usually 10 ---^^^^
      
    • parseFloat if they're decimal fractional numbers

      Example:

      var opening = parseFloat($('#opening').val()); // Always base 10
      
    • The + if you want JavaScript to guess the number base, and give you NaN if there's a non-numeric character

      Example:

      var opening = +$('#opening').val();
      //            ^---- it's hiding here
      

    ...and a few others.

提交回复
热议问题