jQuery 添加变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9029153/
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
jQuery adding variables
提问by Rahul Singh
I have used following code to add three variables but instead of adding these variables its concatenating these variables.
我使用以下代码添加三个变量,但不是添加这些变量,而是连接这些变量。
var registration_fee = $('input[type="radio"][name="registration_fee"]:checked').val();
var material_fee = $('input[type="radio"][name="material_fee"]:checked').val();
var tuition_fee = $('input[type="radio"][name="tuition_fee"]:checked').val();
// alert(tuition_fee)
var total_fee = registration_fee + material_fee + tuition_fee;
$('#total_fee').html(total_fee);
回答by leepowers
Cast them to numbers using parseInt
or parseFloat
:
使用parseInt
或将它们转换为数字parseFloat
:
var total_fee = parseInt(registration_fee) + parseInt(material_fee) + parseInt(tuition_fee);
回答by Sudhir Bastakoti
Try:
尝试:
var total_fee = parseInt(registration_fee, 10) + parseInt(material_fee, 10) + parseInt(tuition_fee, 10);
Or parseFloat, whichever suits
或 parseFloat,以适合的为准
回答by riyas2806299
Try
尝试
Cast them to numbers using Number
使用Number将它们转换为数字
tal_fee = Number(registration_fee) + Number(material_fee) + Number(tuition_fee);
tal_fee = Number(registration_fee) + Number(material_fee) + Number(tuition_fee);
回答by xdazz
Use parseInt
to turn the string to int, or parseFloat
for float.
使用parseInt
打开字符串int或parseFloat
浮法。
回答by subindas pm
Try to use parseInt(price) + parseInt(ticket_buyer_fee)
for the variables it works
尝试使用parseInt(price) + parseInt(ticket_buyer_fee)
它工作的变量