0

我需要更改 javascript 对象中的值。

var wba_product = {
    sku:'12ZB7',
    availability:'Usually ships in 1-2 business days',
}

我想使用 javascript 更改它。

<script language="javascript" type="text/javascript>
if(wba_product.sku == '12ZB7') wba_product.availability + ' Items are charged to your credit card only when they ship. Your credit card will not be charged until the item is shipped to you. ';
</script>

但是这种语法不起作用。

4

3 回答 3

4

两点:

  1. 成员末尾有一个逗号availability,IE 不能容忍这种情况。

  2. 要将值分配给变量和对象成员,请使用= 赋值运算符


var wba_product = {
    sku:'12ZB7',
    availability:'Usually ships in 1-2 business days' // notice the comma removed
}

if(wba_product.sku == '12ZB7') {
  wba_product.availability = 'new value';  // assign a new value
}

如果你想在可用性成员的末尾连接一个字符串,你可以这样做:

 wba_product.availability += ' concatenated at the end.';
于 2009-08-21T20:35:43.363 回答
0

我想你想要的是

if(wba_product.sku == '12ZB7') {
    wba_product.availability += 'Items are charged to...';
}
于 2009-08-21T20:35:09.743 回答
0

据亚马逊的开发人员说。

“你不能这样改变值,你已经直接设置了DOM元素,改变json对象只会改变json对象而不是DOM元素。”

这就是我最终做的事情。我连接了 div.innerHtml += "My added copy";

J。

于 2009-08-25T12:08:08.670 回答