Add two variables using jquery

前端 未结 6 1806
青春惊慌失措
青春惊慌失措 2020-12-07 16:18
var a = 1;
var b = 2;
var c = a+b;

c will show as 12; but I need 3

How do I do it using jQuery?

相关标签:
6条回答
  • 2020-12-07 16:24

    Try this -

    var c = parseInt(a, 10) + parseInt(b, 10);
    
    0 讨论(0)
  • 2020-12-07 16:30
        var a =10;
        var b=10;
        var c=a+b;
        alert(c);
    
        //in  double case
         var e=10.5;
         var f=10.5;
         alert(e+f);
    
         //forcelly convert to int
         alert(parseInt(e)+parseInt(f));
    
         //forcelly convert to float
         alert(parseFloat(a)+parseFloat(b));
    
    
    Example   :
    
     https://jsfiddle.net/v0rjek67/6/
    
    0 讨论(0)
  • 2020-12-07 16:32

    This performs the addition of two variables supplied from input variables selected on the basis of ID.

    var salary, tds, netSalary;
    salary = parseInt($("#txtSalary").val());
    $("#txtTds").on('mouseenter focus', function ()
    {
        tds = parseInt($("#txtSalary").val() * (0.1));
        $("#txtTds").val(tds);
    });
    $("#txtNetSalary").on('mouseenter focus', function () {
        netSalary = parseInt($("#txtSalary").val() +("#txtTds").val());
        $("#txtNetSalary").val(netSalary);
    });
    
    0 讨论(0)
  • 2020-12-07 16:36

    Definitely use jQuery

    Because the power of jQuery is obviously unparalleled, here is how to do with with 100% jQuery:

    var a = 1;
    var b = 2;
    $("<script>c=+("+a+")+ +("+b+")</script>").appendTo($(document));
    

    Now c will hold your result and you used nothing but jQuery! As you can see jQuery is really great because it does all sorts of things

    This also works well because it doesn't matter if a or b are strings!


    Not enough jQuery?

    var a = 1;
    var b = 2;
    $("<script id='test'>$('<textarea id=\\'abc\\'>'+("+a+")+ +("+b+")+'</textarea>').appendTo($('body'))</script>").appendTo('body');
    var c = $("#abc").val();
    

    This answer was done with 100% jQuery because jQuery is awesome but only use this carefully because it might not work sometimes.


    jQuery Arithmetic Plugin

    You can also use the revolutionary jQuery Arithmetic Plugin this has solved world peace in over 4294967295 (>> 0 === -1) countries:

    var a = 1;
    var b = 2;
    var c = $.add(a,b);
    

    while this is all great (this is not confirmed), but in jQuery -3.0.1 I have heard you will be able to add numbers this way:

    $.number($.one, $.two).add($.number($.three, $.four))
    

    this adds 12 (one + two) to 34 (three + four)

    0 讨论(0)
  • 2020-12-07 16:40

    It looks like you have strings and not numbers, you need parseInt() or parseFloat() (if they may be decimals) here, like this:

    var a = "1";
    var b = "2";
    var c = parseInt(a, 10) + parseInt(b, 10);
    //or: var c = parseFloat(a) + parseFloat(b);
    

    You can test the difference here, it's worth noting these are not jQuery but base JavaScript functions, so this isn't dependent on the jQuery library in any way.

    0 讨论(0)
  • 2020-12-07 16:42

    Just try this:

    var a = 1;
    var b = 2;
    var c = (+a) + (+b);
    alert(c); //or whatever you want
    
    0 讨论(0)
提交回复
热议问题