Ste*_*eve 2 javascript jquery numbers
我有一个应该保持运行总计的变量.在此循环的每次传递中,应将金额添加到运行总计中.我必须遗漏一些东西,因为我得到了未定义或NaN.
$('#btnSubmit').click(function(e) {
var totalSqft;
$('.fieldset').each(function() {
var sqft;
var width = $(this).find('input:eq(0)').val();
var height = $(this).find('input:eq(1)').val();
var type = $(this).find('select').val();
if (type == 'tri') {
sqft = (width * height) / 2;
} else {
sqft = (width * height);
};
totalSqft += sqft;
alert('this ' + type + ' is ' + width + ' wide and ' + height + ' high, for a total of ' + sqft + ' square feet');
});
alert('Done. Total Sq Ft is ' + totalSqft);
})?
Run Code Online (Sandbox Code Playgroud)
您需要将值初始化为0:
var totalSqft = 0;
Run Code Online (Sandbox Code Playgroud)
否则,它会被初始化为undefined,而undefined一个数字是NaN.
| 归档时间: |
|
| 查看次数: |
1621 次 |
| 最近记录: |