Javascript 使用 jquery 添加两个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3811678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add two variables using jquery
提问by X10nD
var a = 1;
var b = 2;
var c = a+b;
c
will show as 12
; but I need 3
c
将显示为12
;但是我需要3
How do I do it using jQuery?
我如何使用 jQuery 做到这一点?
回答by Downgoat
Definitely use jQuery
绝对使用jQuery
Because the power of jQuery is obviously unparalleled, here is how to do with with 100% jQuery:
因为 jQuery 的力量显然是无与伦比的,这里是如何使用 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
现在c
将保存您的结果,而您只使用了 jQuery!正如你所看到的,jQuery 真的很棒,因为它可以做各种各样的事情
This also works well because it doesn't matter if a
or b
are strings!
这也很有效,因为无论是字符串a
还是b
字符串都无关紧要!
Not enough jQuery?
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.
这个答案是用 100% jQuery 完成的,因为 jQuery 很棒,但只能小心地使用它,因为它有时可能不起作用。
jQuery Arithmetic Plugin
jQuery 算术插件
You can also use the revolutionary jQuery Arithmetic Pluginthis has solved world peace in over 4294967295 (>> 0 === -1) countries:
您还可以使用革命性的jQuery 算术插件,它已解决了超过 4294967295 (>> 0 === -1) 个国家/地区的世界和平:
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:
虽然这一切都很好(这尚未得到证实),但在 jQuery -3.0.1 中,我听说您可以通过这种方式添加数字:
$.number($.one, $.two).add($.number($.three, $.four))
this adds 12 (one + two) to 34 (three + four)
这将 12(一+二)加到 34(三+四)
回答by Nick Craver
It looks like you have strings and not numbers, you need parseInt()
or parseFloat()
(if they may be decimals) here, like this:
看起来你有字符串而不是数字,你需要parseInt()
或parseFloat()
(如果它们可能是小数)在这里,像这样:
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.
您可以在此处测试差异,值得注意的是这些不是 jQuery 而是基本的 JavaScript 函数,因此这不以任何方式依赖于 jQuery 库。
回答by Sachin Shanbhag
Try this -
尝试这个 -
var c = parseInt(a, 10) + parseInt(b, 10);
回答by Ananth Vivekanand
Just try this:
试试这个:
var a = 1;
var b = 2;
var c = (+a) + (+b);
alert(c); //or whatever you want
回答by Mohit Singh
This performs the addition of two variables supplied from input variables selected on the basis of ID.
这会执行根据 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);
});
回答by Mahabub Rabbani
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/