gpt4 book ai didi

javascript - 我怎样才能结合这两个jquery计算?

转载 作者:行者123 更新时间:2023-12-03 09:18:05 25 4
gpt4 key购买 nike

我有一些输入,其中有两个宽度和两个高度的英尺/英寸。我必须将它们转换为所有英尺并获得平方英尺。例如,宽度为 1'2",高度为 1'2"。另一种宽度为 2'1",高度为 2'1"。我必须将宽度转换为英尺,然后乘以它们的高度,然后乘以第一个宽度和高度,然后加上第二个宽度和高度。此外,每一边都需要在上面加上 1.6 的乘法。 我已经用第一个宽度和高度完成了它,但我认为对第二个宽度和高度进行重复是重复的,但从那以后不太知道将它们组合在一起的最佳方法是什么。

我的js有这样的东西

$('.total-footage').on('click', function(){
/*Side A Calculation*/
var $sideAFootValueW = parseInt($('.dimensions-side-a .dimension-width .dimension-input-foot').val());
var $sideAInchValueW = parseInt($('.dimensions-side-a .dimension-width .dimension-input-inch').val());

if(($sideAFootValueW == "") || (!$.isNumeric($sideAFootValueW))){
$sideAFootValueW = 0;
}

if(($sideAInchValueW == "") || (!$.isNumeric($sideAInchValueW))){
$sideAInchValueW = 0;
}else{
$sideAInchValueW = $sideAInchValueW/12;
}

var totalFootAW = $sideAFootValueW + $sideAInchValueW;

var $sideAFootValueH = parseInt($('.dimensions-side-a .dimension-height .dimension-input-foot').val());
var $sideAInchValueH = parseInt($('.dimensions-side-a .dimension-height .dimension-input-inch').val());

if(($sideAFootValueH == "") || (!$.isNumeric($sideAFootValueH))){
$sideAFootValueH = 0;
}

if(($sideAInchValueH == "") || (!$.isNumeric($sideAInchValueH))){
$sideAInchValueH = 0;
}else{
$sideAInchValueH = $sideAInchValueH/12;
}

var totalFootAH = $sideAFootValueH + $sideAInchValueH;

var sideATotal = totalFootAH * totalFootAW * 1.6;

/*Side B Calculation*/
var $sideBFootValueW = parseInt($('.dimensions-side-b .dimension-width .dimension-input-foot').val());
var $sideBInchValueW = parseInt($('.dimensions-side-b .dimension-width .dimension-input-inch').val());

if(($sideBFootValueW == "") || (!$.isNumeric($sideBFootValueW))){
$sideBFootValueW = 0;
}

if(($sideBInchValueW == "") || (!$.isNumeric($sideBInchValueW))){
$sideBInchValueW = 0;
}else{
$sideBInchValueW = $sideBInchValueW/12;
}

var totalFootBW = $sideBFootValueW + $sideBInchValueW;

var $sideBFootValueH = parseInt($('.dimensions-side-b .dimension-height .dimension-input-foot').val());
var $sideBInchValueH = parseInt($('.dimensions-side-b .dimension-height .dimension-input-inch').val());

if(($sideBFootValueH == "") || (!$.isNumeric($sideBFootValueH))){
$sideBFootValueH = 0;
}

if(($sideBInchValueH == "") || (!$.isNumeric($sideBInchValueH))){
$sideBInchValueH = 0;
}else{
$sideBInchValueH = $sideBInchValueH/12;
}

var totalFootBH = $sideBFootValueH + $sideBInchValueH;

var sideBTotal = totalFootBH * totalFootBW * 1.6;

$('.total').empty();
$('.total').append(sideATotal+sideBTotal);

});

我认为我应该有一个函数,这样即使我需要计算更多边,也可以轻松完成此计算

最佳答案

将两个帮助函数添加到您的代码中

function getSideDimensions($side){
var width = getDimension($side.find('.dimension-width'));
var height = getDimension($side.find('.dimension-height'));
return width * height * 1.6;
}

function getDimension($dimension){
var feet = $dimension.find('.dimension-input-foot');
var inches = $dimension.find('.dimension-input-inch');

if((feet == "") || (!$.isNumeric(feet))){
feet = 0;
}

if((inches == "") || (!$.isNumeric(inches))){
inches = 0;
}else{
inches = inches/12;
}
return feet + inches;
}

并将您的代码更改为

$('.total-footage').on('click', function(){
var sideATotal = getSideDimensions($('.dimensions-side-a'));
var sideBTotal = getSideDimensions($('.dimensions-side-b'));
$('.total').empty();
$('.total').append(sideATotal+sideBTotal);
}

关于javascript - 我怎样才能结合这两个jquery计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31910329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com