Converting string to number in javascript/jQuery

后端 未结 9 1864
攒了一身酷
攒了一身酷 2021-01-31 13:08

Been trying to convert the following to number:

相关标签:
9条回答
  • 2021-01-31 13:56

    You can adding a + before the string without using parseInt and parseFloat and radix, Simply

    sample:

    var votevalue = +$('button').data('votevalue');
    
    alert(typeof(votevalue));
    
    0 讨论(0)
  • 2021-01-31 14:02

    var string = 123 (is string),

    parseInt(parameter is string);

    var string = '123';
    
    var int= parseInt(string );
    
    console.log(int);  //Output will be 123.
    
    0 讨论(0)
  • 2021-01-31 14:07

    You should just use "+" before $(this). That's going to convert the string to number, so:

    var votevalue = +$(this).data('votevalue');
    

    Oh and I recommend to use closest() method just in case :)

    var votevalue = +$(this).closest('.btn-group').data('votevalue');
    
    0 讨论(0)
提交回复
热议问题