2013-02-26 29 views
0

以下是標題。 它將結果顯示在新頁面中,而我想在運行代碼的同一頁面顯示結果。請幫助。從下拉列表中獲取值並在Java腳本的同一頁面中顯示結果

function calculate() { 
    var width = document.getElementById("width"); 
    width1 = width.options[width.selectedIndex].text; 
    var height = document.getElementById("height"); 
    var height1 = height.options[height.selectedIndex].text;  
    var calculate = width1 * height1 * 3; 
    result.innerHTML = "<div>" + calculate + "</div>"; 
} 

Choose Width 
<select id="width" name="height" onchange="calculate()"> 
    <option>1</option> 
    <option>1</option> 
</select> 
<br/> 
Choose Heiht 
<select id="height" name="height" onchange="calculate()"> 
    <option>1</option> 
    <option>2</option>  
</select> 
+1

意味着什麼下一頁 – PSR 2013-02-26 07:04:36

+0

OP的意思是說,結果會在下一頁提交,但他希望在同一頁上更新/顯示結果 – asifsid88 2013-02-26 07:12:28

回答

1

你可以使用:

Choose Width: 

<select id="width" name="height" onchange="calculate()"> 
    <option>1</option> 
    <option>2</option> 
</select> 

    <br/> 

    Choose Height 

<select id="height" name="height" onchange="calculate()"> 
    <option>1</option> 
    <option>2</option>  
</select> 
<div id="result"></div> 

和JavaScript應該是:

function calculate() 
{ 
    var width = document.getElementById("width"); 
    width1 = width.options[width.selectedIndex].text; 
    var height = document.getElementById("height"); 
    var height1 = height.options[height.selectedIndex].text;  
    var calculate = width1*height1*3; 
    var result = document.getElementById("result"); 
    result.innerHTML = calculate; 
} 
+0

感謝兄弟,它完成了工作......感謝您的幫助 – 2013-02-26 07:11:27

+0

如果它對您有幫助,請接受答案!只需在左邊標記答案:) – 2013-02-26 07:11:57

+0

是的,它幫助我。左邊的刻度線:) – 2013-02-26 09:23:04

0

你應該有result作爲一個div在HTML代碼和那麼你需要做內心HTML或的innerText把你的結果
添加新標籤(在你的HTML代碼)

<div id="result"></div> 

現在JS更新這個div元素

var result = document.getElementById("result"); 
result.innerText = calculate; 
+0

這就是我剛纔所說的.. – 2013-02-26 07:11:32

+0

是啊,這也是一樣。謝謝你的幫助..你也是 – 2013-02-26 09:24:08

相關問題