2014-10-01 91 views
0

我在做一個帶有Javascript的程序。我必須在這個程序中做一些操作,最後,我必須在表單中顯示結果。在html表單中顯示變量的內容(javascript)

我該怎麼辦?我認爲也許我必須在「id」或「value」參數中加入某些內容,但是,我不確定!

謝謝!

這裏我讓你看看我的一些代碼。 (這不是所有的代碼,這只是一部分,我認爲在什麼我質疑影響)

function loadPage() 
 
{ \t 
 
\t var divContent = "<br/>This is a calculator. Introcuce 2 numbers and an operator. "; 
 
\t divContent += "<input type = 'text' id='number1' value='0'/> "; 
 
     divContent += "<input type = 'text' id='operator' value=''/> ";  
 
     divContent += "<input type = 'text' id='number2' value='0'/>"; 
 
\t divContent += "<input type = 'button' value='operations' onclick='operations()'/>"; 
 
\t 
 
\t document.getElementById("content").innerHTML += divContent; 
 
} 
 
\t 
 
function operations() 
 
{ 
 
\t 
 
\t var number1 = document.getElementById("number1").value; 
 
     var operator = document.getElementById("operator").value; 
 
     var number2 = document.getElementById("number2").value; 
 
\t var divContent="<br><br><br> RESULT: "; 
 
     
 
     switch(operator) 
 
     { 
 
      case '+': 
 
       
 
       result = number1 + number2; 
 
           
 
       var divContent="The result of "+ number1 +" + "+number2+" = "; 
 
       divContent += "<input type = 'text' id='operator' value=''/>"; 
 
       break; 
 
      } 
 
    }

回答

0

這是你的工作合作德:

var divContent = "<br/>This is a calculator. Introcuce 2 numbers and an operator. "; 
 
\t divContent += "<input type = 'text' id='number1' value='0'/> "; 
 
     divContent += "<input type = 'text' id='operator' value=''/> ";  
 
     divContent += "<input type = 'text' id='number2' value='0'/>"; 
 
\t divContent += "<input type = 'button' value='operations' onclick='operations()'/>"; 
 
\t 
 
\t document.body.innerHTML += divContent; 
 

 
function operations() 
 
{ 
 
\t var number1 = document.getElementById("number1").value; 
 
    var operator = document.getElementById("operator").value; 
 
    var number2 = document.getElementById("number2").value; 
 
\t var divContent="<br><br><br> RESULT: "; 
 
    document.body.innerHTML += divContent;   
 
     switch(operator) 
 
     { 
 
      case '+': 
 
       
 
       result = parseInt(number1,10) + parseInt(number2,10); 
 
           
 
       var divContent="The result of "+ number1 +" + "+number2+" = "; 
 
       divContent += "<input type = 'text' id='operator' value='"+result+"'/>"; 
 
       document.body.innerHTML += divContent; 
 
       break; 
 
     } 
 
    }

0

一般:您可以連接字符串與string = "..." + text;

如果你想要把結果在新的輸入標籤,使用以下命令:

divContent += "<input type = 'text' id='operator' value='"+ result +"'/>";