Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以在jQuery(JavaScript)中更改var吗?_Javascript_Jquery - Fatal编程技术网

我可以在jQuery(JavaScript)中更改var吗?

我可以在jQuery(JavaScript)中更改var吗?,javascript,jquery,Javascript,Jquery,在AJAX调用之后,我得到如下JSON数据: { "detailPrice": { "server": { "amount": "8", "usedPrice": "10", "discountPrice": "-1", "totalPrice": "9" }, "network": { "amount": "8", "usedPrice": "10", "discountPrice"

在AJAX调用之后,我得到如下JSON数据:

{
  "detailPrice": {
    "server": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "network": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "storage": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "loadBalancer": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "others": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "support": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "totalPrice": {
      "totalUsedPrice": "8",
      "totalDiscountPrice": "-2",
      "missedPrice": "10",
      "tax": "9",
      "otherDiscount": "-1"
    }
  }
}
var serverpriceHTML = "총 " + result.detailPrice.server.amount + "대<br/>";
serverpriceHTML += "이용요금 " + result.detailPrice.server.usedPrice + "원<br/>";
serverpriceHTML += "할인요금 " + result.detailPrice.server.discountPrice + "원<br/>";
var serverTotalPriceHTML = result.detailPrice.server.totalPrice + "원";
$("#server_price").html(serverpriceHTML);
$("#server_totalprice").html(serverTotalPriceHTML);

var networkpriceHTML = "총 " + result.detailPrice.network.amount + "대<br/>";
networkpriceHTML += "이용요금 " + result.detailPrice.network.usedPrice + "원<br/>";
networkpriceHTML += "할인요금 " + result.detailPrice.network.discountPrice + "원<br/>";
var networkTotalPriceHTML = result.detailPrice.network.totalPrice + "원";
$("#network_price").html(networkpriceHTML);
$("#network_totalprice").html(networkTotalPriceHTML);
我必须将这些数据附加到视图中,因此我编写了如下代码:

{
  "detailPrice": {
    "server": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "network": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "storage": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "loadBalancer": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "others": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "support": {
      "amount": "8",
      "usedPrice": "10",
      "discountPrice": "-1",
      "totalPrice": "9"
    },
    "totalPrice": {
      "totalUsedPrice": "8",
      "totalDiscountPrice": "-2",
      "missedPrice": "10",
      "tax": "9",
      "otherDiscount": "-1"
    }
  }
}
var serverpriceHTML = "총 " + result.detailPrice.server.amount + "대<br/>";
serverpriceHTML += "이용요금 " + result.detailPrice.server.usedPrice + "원<br/>";
serverpriceHTML += "할인요금 " + result.detailPrice.server.discountPrice + "원<br/>";
var serverTotalPriceHTML = result.detailPrice.server.totalPrice + "원";
$("#server_price").html(serverpriceHTML);
$("#server_totalprice").html(serverTotalPriceHTML);

var networkpriceHTML = "총 " + result.detailPrice.network.amount + "대<br/>";
networkpriceHTML += "이용요금 " + result.detailPrice.network.usedPrice + "원<br/>";
networkpriceHTML += "할인요금 " + result.detailPrice.network.discountPrice + "원<br/>";
var networkTotalPriceHTML = result.detailPrice.network.totalPrice + "원";
$("#network_price").html(networkpriceHTML);
$("#network_totalprice").html(networkTotalPriceHTML);
我必须将
服务器
更改为
网络
存储
负载平衡器
等以获取数据,但我不确定如何更改此设置

如果我喜欢:

function makeHTML(price, totalPrice, name) {
  var test = "result.detailPrice" + name + ".amount";
  var serverpriceHTML = "총 " + test + "대<br/>";
  serverpriceHTML += "이용요금 " + result.detailPrice.server.usedPrice + "원<br/>";
  serverpriceHTML += "할인요금 " + result.detailPrice.server.discountPrice + "원<br/>";  
  var serverTotalPriceHTML = result.detailPrice.server.totalPrice + "원";

  $(price).html(serverpriceHTML);
  $(totalPrice).html(serverTotalPriceHTML);
}
函数makeHTML(价格、总价、名称){
var test=“result.detailPrice”+name+“.amount”;
var serverpriceHTML=”총 " + 测试+”대
“; serverpriceHTML+=”이용요금 “+result.detailPrice.server.usedPrice+”원
“; serverpriceHTML+=”할인요금 “+result.detailPrice.server.折扣价格+”원
“; var serverTotalPriceHTML=result.detailPrice.server.totalPrice+”원"; $(price).html(serverpriceHTML); $(totalPrice).html(serverTotalPriceHTML); }

这只是添加字符串测试。有什么好主意吗?

当您想使用字符串值时,请使用
[]
符号来访问值

而不是

var test = "result.detailPrice" + name + ".amount";
试一试


如果要替换变量值,则点表示法不起作用

简单示例

var obj = {
       "ball" : "bat"
    };

var name = "ball";
//如果你使用点符号

对象名称
(提供未定义的内容)
(它将尝试获取get the
key=name
)它不会替换变量名

//[]括号表示法

obj[name]
(给你bat)

它将替换变量名并获取值

您可以指定不同的变量值,例如:-

//This will give you some private variables for following:-
var constants={
server=function(){return 0},
network=function(){return 1},
storage=function(){return 2},
loadBalancer=function(){return 3},
others=function(){return 4},
suport=function(){return 5},
totalPrice=function(){return 6}
}
然后,您可以在一个变量中使用JSON,如下所示:-

您可以按如下方式更改JSON:-

var jsonData= {
           "detailPrice":{
              {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "totalUsedPrice":"8",
                 "totalDiscountPrice":"-2",
                 "missedPrice":"10",
                 "tax":"9",
                "otherDiscount": "-1"
              }

           }
        }
jsonData.detailPrice[constant.server];//For Server Details
jsonData.detailPrice[constant.network];//For Network Details
然后按如下方式调用它:-

var jsonData= {
           "detailPrice":{
              {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "amount":"8",
                 "usedPrice":"10",
                 "discountPrice":"-1",
                 "totalPrice":"9"
              },
                {
                 "totalUsedPrice":"8",
                 "totalDiscountPrice":"-2",
                 "missedPrice":"10",
                 "tax":"9",
                "otherDiscount": "-1"
              }

           }
        }
jsonData.detailPrice[constant.server];//For Server Details
jsonData.detailPrice[constant.network];//For Network Details

以此类推。希望这听起来有帮助。

为什么不使用for循环来迭代JSON?如果要替换变量值,点表示法不起作用。@user1765884..很高兴能提供帮助:)。同时检查示例