2015-07-12 126 views
0

所以我爲夏季班做了這個,我得讓這個地毯計算器在「執行一個document.write來顯示結果到一個新的html頁面中顯示結果「。 (直接從作業中引用)。它說要使用document.write,並在w3上使用一些教程後掌握document.write的概念,但是當我嘗試將其應用於我的項目時,它對我的​​網頁沒有任何影響。HTML + Javascript document.write不能正常工作

下面是我的代碼:

<?xml version="1.0" encoding="utf-8" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<link rel="stylesheet" href="http://www.w3.org/StyleSheets/Core/Oldstyle" type="text/css" /> 

<head> 
<title>Tutorial Project 10</title> 
</head> 
<body> 

<h1>Carpet Calculator</h1> 

<form name = "carpet" action=" "> 

</br>Enter the length of your room in feet</br><input name = "length" type = "text" /> 

</br>Enter the width of your room in feet</br><input name = "width" type = "text" /> 
</br>Typically an allowance is made for room irregularities and unavoidable waste. 
</br>Enter the percent overage as an integer in the interval [0, 20]</br><input name = "overage" type = "text" /> 

</br></br><input name = "SqFt" type = "button" value = "Compute Square Feet " onclick = "ComputeSquareFeet()" /> 

</br></br><input name = "SqYd" type = "button" value = "Compute Square Yards" onclick = "ComputeSquareYards()" /> 


</br></br><input type = "reset" value = "Clear" /> 

</form> 

<script type="text/javascript"> 

     function ComputeSquareFeet() 
     { 
      var SqFt = (carpet.length.value*carpet.width.value); 
      carpet.SqFtResult.value = SqFt+(SqFt*(carpet.overage.value/100)); 
      document.write(SqFtResult); 
     } 




     function ComputeSquareYards() 
     { 
      var SqYd = ((carpet.length.value/3)*(carpet.width.value/3)); 
      carpet.SqYdResult.value = SqYd+(SqYd*(carpet.overage.value/100)); 
      document.write(SqYdResult); 
     } 


</script> 

</body> 
</html> 

頁是這樣的,用戶輸入的數據爲3盒,按下按鈕,它調用的函數,並在該函數結束,它執行文件。寫。然而,在做了一堆試驗和錯誤之後,我得出結論說我的公式正在工作,函數被調用,但document.write並不是由於某種原因。

關於我在做什麼錯的任何想法?謝謝!

編輯:我有這樣的顯示功能的結果,只是爲了確保它是工作,但一旦我切換到文件撰寫,似乎沒有什麼工作

+0

你在頁面加載後使用document.write –

+0

@JaromandaX那我該如何解決呢? http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_doc_write3我不明白它在這裏的做法與我的不同 – DrMoney

回答

0

在我看來,一個文本框,有問題不是真的document.write,但可能是你從窗體中獲取數據的方式。

它應該是這樣的:

document.forms["formname"][fieldname].value; 

也營造出variabe不喜歡它:

var SqYdResult=somthing; 

希望它可以幫助

+0

我無法弄清楚如何在評論中發佈我的代碼,但我嘗試了你的建議,但它不起作用 – DrMoney

1

好像你需要寫這一個新的HTML頁面;例如嘗試調用下面的功能與您的文字:

function writeToNewPage(text) { 
    var newPage = window.open("", "New Page","width=400, height=300, scrollbars=1, resizable=1"); 

    newPage.document.open(); 
    newPage.document.write(text); 
    newPage.document.close(); 
} 

你不能沒有擦整個頁面的當前頁面上使用文件撰寫,它似乎分配問題不實說了新的一頁。

+0

它專門用於使用document.write。我認爲我的教授打算讓整個頁面消失,只剩下答案。不過,我只是試着看你的代碼是否可以工作,但什麼也沒有發生。我認爲我的問題可能在其他地方,而document.write是好的。 我有一個文本框,顯示功能的結果只是爲了確保它工作,但一旦我切換到文檔。寫,似乎沒有任何工作 – DrMoney

+0

不知道你將如何取代當前頁面。如果您使用Chrome或IE,則可以按F12調出開發人員工具欄,然後在控制檯中鍵入document.write(「test」)。你可以看到它似乎沒有做任何事情。 –

+0

打開一個新窗口應該可以工作,並且您正在使用document.write。不過,您可能會收到關於彈出窗口的警告。 –

0

原來我自己走在前面的

function ComputeSquareFeet() 
{ 
    var SqFt = (carpet.length.value*carpet.width.value); 
    carpet.SqFtResult.value = SqFt+(SqFt*(carpet.overage.value/100)); 
    document.write(SqFtResult); 
} 

需求是

function ComputeSquareFeet() 
{ 
    var SqFt = (carpet.length.value*carpet.width.value); 
    var SqFtResult = SqFt+(SqFt*(carpet.overage.value/100)); 
    document.write("Carpet needed in square feet: " + SqFtResult); 
} 

,因爲我沒有宣佈它不工作的變量。當我定義它時,我得到了我想要的結果。

謝謝大家的幫助!