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

2 つのポイント:

  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 に答える