Javascript 使用Jquery检索JSON值以进行计算
我有一个计算表单,它的值来自JSONJavascript 使用Jquery检索JSON值以进行计算,javascript,json,Javascript,Json,我有一个计算表单,它的值来自JSON var shirtsJSON = [ {"pattern": "Delta Adult S/S 5.2oz", "basePrice": 5.68, "sizes": {"s-xl": 0, "xxl": 0.84}, "colors": {"white": 0, "athletic": 0.12, "color": 0.23}, "numColors": {"1
var shirtsJSON = [
{"pattern": "Delta Adult S/S 5.2oz", "basePrice": 5.68,
"sizes": {"s-xl": 0, "xxl": 0.84},
"colors": {"white": 0, "athletic": 0.12, "color": 0.23},
"numColors": {"1-2": 0, "3-4": 1.60, "5-6": 3.18, "7-8": 4.81, "9-10": 6.39},
"deductions": {"de48pp": 0, "de72pp": .9, "de96pp": 1.35, "de144pp": 2.37, "de288pp": 2.65},
"oneLocation": {"onelocnone": 0, "oneloc12": 3.28, "oneloc34": 5.41, "oneloc56": 7.52, "oneloc78": 9.69, "oneloc910": 11.80},
"twoLocation": {"twolocnone": 0, "twoloc12": 3.28, "twoloc34": 5.41, "twoloc56": 7.52, "twoloc78": 9.69, "twoloc910": 11.80},
"threeLocation": {"threelocnone": 0, "threeloc12": 3.28, "threeloc34": 5.41, "threeloc56": 7.52, "threeloc78": 9.69, "threeloc910": 11.80},
"fourLocation": {"fourlocnone": 0, "fourloc12": 3.28, "fourloc34": 5.41, "fourloc56": 7.52, "fourloc78": 9.69, "fourloc910": 11.80}},
{"pattern": "Delta Adult S/S 6.1oz", "basePrice": 6.68,
"sizes": {"s-xl": 0, "xxl": 0.84},
"colors": {"white": 0, "athletic": 0.12, "color": 0.23},
"numColors": {"1-2": 0, "3-4": 1.60, "5-6": 3.18, "7-8": 4.81, "9-10": 6.39},
"deductions": {"de48pp": 0, "de72pp": .70, "de96pp": 1.25, "de144pp": 2.47, "de288pp": 2.55},
"oneLocation": {"onelocnone": 0, "oneloc12": 3.28, "oneloc34": 5.41, "oneloc56": 7.52, "oneloc78": 9.69, "oneloc910": 11.80},
"twoLocation": {"twolocnone": 0, "twoloc12": 3.28, "twoloc34": 5.41, "twoloc56": 7.52, "twoloc78": 9.69, "twoloc910": 11.80},
"threeLocation": {"threelocnone": 0, "threeloc12": 3.28, "threeloc34": 5.41, "threeloc56": 7.52, "threeloc78": 9.69, "threeloc910": 11.80},
"fourLocation": {"fourlocnone": 0, "fourloc12": 3.28, "fourloc34": 5.41, "fourloc56": 7.52, "fourloc78": 9.69, "fourloc910": 11.80}},
最后的结果在这里
$('#48pp').html("Per Piece : " + currency + getMoney(totalPrice));
$('#72pp').html("Per Piece : " + currency + getMoney(totalPrice));
$('#96pp').html("Per Piece : " + currency + getMoney(totalPrice));
$('#144pp').html("Per Piece : " + currency + getMoney(totalPrice));
$('#288pp').html("Per Piece : " + currency + getMoney(totalPrice));
我想让它从我尝试过的扣减中减去值:
$('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON.deductions[0].de72pp));
但它不起作用,我似乎无法理解。有人能帮忙吗?
shirtsJSON。扣减不是数组,因此尝试索引不会检索任何内容
请尝试使用shirtsJSON.declarations.de72pp
,因为de72pp
是shirtsJSON.declarations
对象的属性
另外,从var shirtsJSON=[{
行中删除[
。shirtsJSON。扣减
不是数组,因此尝试索引不会检索任何内容
请尝试使用shirtsJSON.declarations.de72pp
,因为de72pp
是shirtsJSON.declarations
对象的属性
另外,从var shirtsJSON=[{
行中删除[
。我认为[0]的位置错误-请在shirtsJSON
之后尝试,而不是扣减
:
$('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[0].deductions.de72pp));
而不是:
这是因为shirtsJSON
是一个数组,而declarations
不是
编辑:
检索多个扣减项的循环的快速示例。这假设每个扣减项每次包含相同的信息(例如,它们都包含48pp
,72pp
等)
for(i=0;i
注意i
如何替换shirtsJSON[0]
中的0
。您需要稍微更改代码,使其.append
ed(添加到该元素中的任何内容的末尾),而不是使用.html
(覆盖该元素中的任何内容)
编辑2:
这里有一个您可能会觉得有用的链接:我认为[0]的位置不对-请在shirtsJSON
之后尝试,而不是扣减
:
$('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[0].deductions.de72pp));
而不是:
这是因为shirtsJSON
是一个数组,而declarations
不是
编辑:
检索多个扣减项的循环的快速示例。这假设每个扣减项每次包含相同的信息(例如,它们都包含48pp
,72pp
等)
for(i=0;i
注意i
如何替换shirtsJSON[0]
中的0
。您需要稍微更改代码,使其.append
ed(添加到该元素中的任何内容的末尾),而不是使用.html
(覆盖该元素中的任何内容)
编辑2:
这里有一个你可能会觉得有用的链接:我想你想要shirtsJSON[0]。演绎
——如果不看你的代码和JSON的其余部分,很难判断。你的JSON格式不正确。请看:我想你想要shirtsJSON[0].Declarations
——如果看不到代码的其余部分和JSON,就很难判断。您的JSON格式不正确。请看:我还有一个问题……如果我想有多个具有不同推断的模式,那么代码就不起作用了。我需要它有所不同吗?我想索引是导致它的原因。我还是有点新在您的示例中,shirtsJSON
是一个索引数组(我假设它代表一个产品),包含多个关联数组(每个数组代表同一产品的不同模式)。要获取每个模式的扣减,您可以循环使用shirtsJSON
索引数组,可能会使用for
循环,在执行过程中检索每个模式的信息。您有没有我可以看到的示例?每当我添加无法使其正常工作时,都会在回答中添加一个示例。我将向您展示我的w孔代码。可能是什么原因导致它断裂?这是我的另一个问题…如果我想有多个具有不同推断的模式,那么代码就不起作用了。我需要它不同吗?我猜索引是导致它断裂的原因。我还是有点陌生,如果这是一个愚蠢的问题,很抱歉。在你的例子中,shirtsJSON是一个索引数组(我假设它代表一个产品),包含多个关联数组(每个数组代表同一产品的不同模式)。要获取每个模式的扣减,您可以循环使用shirtsJSON
索引数组,可能会使用for
循环,在执行过程中检索每个模式的信息。您有没有我可以看到的示例?每当我添加无法使其正常工作时,都会在回答中添加一个示例。我将向您展示我的w孔代码。可能是什么原因导致它破裂?这是答案
for(i = 0; i < shirtsJSON.length; i++) {
$('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[i].deductions.de72pp));
...etc...
}