2012-10-11 53 views
0

我無法獲得價格自動更新。我無法使用.innerHTML更新價格

要製作唯一的ID;對於價格,我用:

id="target_1_<?php echo $product['product_id']; ?>" 

而對於選擇菜單使用:

id="select_<?php echo $product['product_id']; ?>" 

下面是HTML/PHP

<?php foreach ($products as $product) { ?> 
    <span id="target_1_<?php echo $product['product_id']; ?>"><?php echo $product['price']; ?></span> 
<?php } ?> 


<?php if ($product['options']) { ?> 
    <?php foreach ($product['options'] as $option) { ?> 
    <?php if ($option['type'] == 'select') { ?> 

    <select name="option[<?php echo $option['product_option_id']; ?>]" class="select_options" id="select_<?php echo $product['product_id']; ?>" onchange="getIt(this);"> 

    <?php foreach ($option['option_value'] as $option_value) { ?> 
    <option value="<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?> 

    <?php if ($option_value['price']) { ?> 
    (<?php echo $option_value['price_prefix']; ?><span id="newPrice"><?php echo $option_value['price']; ?></span>) 
    <?php } ?> 
    </option> 
    <?php } ?> 
    </select> 
<?php } ?> 

........ 
<?php } ?> 

然後爲我用這個的onchange功能:

function getIt(el) { 
var a = el.options[el.selectedIndex].text; 
var position1 = a.indexOf("("); 
var position2 = a.indexOf(")"); 
var position1 = position1+2 
var finalA = a.substring(position1, position2); 
document.getElementById("target_1_<?php echo $product['product_id']; ?>").innerHTML = "$"+finalA; 
} 

alert(finalA)給出值I需要,我似乎無法得到它放回DOM

+0

檢查'「target_1 _ 「'通過alert的這個值 – XMen

+1

JavaScript不能執行PHP代碼。嘗試一個不使用PHP –

回答

1

變化這兩個:在GETIT功能 通的product_id

<select name="option[<?php echo $option['product_option_id']; ?>]" class="select_options" id="select_<?php echo $product['product_id']; ?>" onchange="getIt(this,<?php echo $product['product_id']; ?>);"> 

使用ID與產品ID參數concanate,而不是使用PHP

function getIt(el, productid) { 
var a = el.options[el.selectedIndex].text; 
var position1 = a.indexOf("("); 
var position2 = a.indexOf(")"); 
var position1 = position1+2 
var finalA = a.substring(position1, position2); 
document.getElementById("target_1_"+productid).innerHTML = "$"+finalA; 
} 
+1

周壓力解除的ID系統。 – Travis

0

試試這個:

function getIt(el) { 
var a = el.options[el.selectedIndex].text; 
var position1 = a.indexOf("("); 
var position2 = a.indexOf(")"); 
var position1 = position1+2; //BTW , Your forgot ; over here 
var product_id = <?php echo $product['product_id']; ?>; 
//You always can use: console.log(product_id); 
//Then check your browser's console to see this variable value 
//In this case , just View Source and make sure that instead of the php line there's a valid 
// number (the product's id) 
var finalA = a.substring(position1, position2); 
document.getElementById("target_1_"+product_id).innerHTML = "$"+finalA; 
} 
0

添加額外的數據attribut ?在選擇標籤E對於產品ID喜歡data-product-id

<select name="option[<?php echo $option['product_option_id']; ?>]" 
    data-product-id = "target_1_<?php echo $product['product_id']; ?>" 
    class="select_options" id="select_<?php echo $product['product_id']; ?>" 
    onchange="getIt(this);"> 

在Javascript中

function getIt(e) { 
    //Your part 
    document.getElementById(e.getAttribute("data-product-id")).innerHTML = "$"+finalA; 
}