2014-04-09 72 views
2

我討厭來這裏看似簡單的問題來解決,但我很難過,並嘗試了多種解決方案。Javascript innerHTML更改不起作用

我有一個複選框,所以點擊時他們將運行一個函數,將改變購物車的總價格,但我甚至不能在文本框中獲得一個簡單的innerHTML更改,它將顯示onclick的總價格事件。

HTML/PHP:

<?php 

.... 
.... 
echo "<span class='chosen'><input type='checkbox' name='event[]' 
value='{$event['eventID']}' title='{$event['eventPrice']}' 
onclick='price_calc(this.title, this)'/></span>" 

?> 

<div id="checkCost"> 
    <h2>Total cost</h2> 
    Total <input type="text" name="total" id="total" size="10" readonly="readonly" /> 
</div> 
.... 
.... 
<script src="bookingJS.js"></script> 
</body> 

正如你所看到的元素都被加載到DOM的JavaScript運行之前。

JS:

function price_calc(price, chkbx) { 

    document.getElementById('total').innerHTML = 'Hello'; 
    alert(document.getElementById('total').innerHTML); 

} 

感謝您事先的任何提示。

回答

4

「總」 是一個field.So,嘗試document.getElementById('total').value

innerHTML的是對非現場元素,如跨度,DIV等。

4

要設置input[type=text]值,可以使用.valueinnerHTML

function price_calc(price, chkbx) { 
    document.getElementById('total').value = 'Hello'; 
    alert(document.getElementById('total').value); 
} 
0

試試這個:

document.getElementById('total').value="Hello"; 
alert(document.getElementById('total').value);