2012-09-19 99 views
0

我仍在使用JavaScript編碼的計算器。我問這個問題,我該如何使它停止顯示輸出警報,並將其打印到名爲「Output」下面的div標記中。將警報更改爲打印輸出

我已經放在我認爲這將是在輸出和我有這樣的粘貼下面:

<html> 
    <head> 
    <link href="stylesheet/stylesheet.css" rel="stylesheet" type="text/css"> 

    <script type="text/javascript"> 



    function checkinputs(){ 
    var  A = parseInt(document.triangleform.input1.value); 
    var  B = parseInt(document.triangleform.input2.value); 
    var  C = parseInt(document.triangleform.input3.value); 


     if (A == B && B == C) { alert("Equalateral"); } 
     if (A == B && B != C || B == C && A != B || C == A && A != B) {alert("Isosceles");} 
     if (A != B && B != C && C != A) {alert("Scalene!");} 
    } 



    function keypress(evt) { 
     var theEvent = evt || window.event; 
     var key = theEvent.keyCode || theEvent.which; 
     key = String.fromCharCode(key); 
     var regex = /[0-9]|\./; 
     if(!regex.test(key)) { 
     theEvent.returnValue = false; 
     if(theEvent.preventDefault) theEvent.preventDefault(); 
     } 
    } 

    function notEmpty(field, NocharMsg){ 
     if(field.value.length == 0){ 
      alert(NocharMsg); 
      field.focus(); 
      return false; 
     } 
     return true; 
    } 
    </script 


    </head> 
    <body> 
    <div id="Container"> 

    <div id="Header"><h1></h1></div> 

      <div id="Content_1"> 
        <div id="Explanation"> 
        This calculator will determine what 
        triangle you have made depending on 
        the integer values in the input fields. 

        </div> 
        <div id="Form"> 
         <FORM NAME="triangleform"> 
         Enter the triangle values below: <br> 
         <p> 
         <h4>Side 1: </h4><BR> 
         <INPUT TYPE="Integer" NAME="input1" onkeypress='keypress(event)' /><P> 
         <h4>Side 2: </h4><BR> 
         <INPUT TYPE="Integer" NAME="input2" onkeypress='keypress(event)' /><P> 
         <h4>Side 3:</h4> <BR> 
         <INPUT TYPE="Integer" NAME="input3" onkeypress='keypress(event)' /><P> 
         <INPUT TYPE="button" NAME="Submit" Value="Submit" onClick="checkinputs()" /> 
         </FORM> 
        </div> 
        <div id="Verbal_Output"> 
         <h2>You made a:</h2> 
         <p>   
         <h2>Triangle</h2> 
        </div> 

       </div> 
       <p> 
       <p> 
      <div id="Content_2"> 

      <div id="Output"> 
      <script> 
      document.getElementById("Output").value = checkinputs(); 

      </script> 
      </div> 



    </body> 
    </html> 

非常感謝你。

回答

2

快到了 - 一個div沒有value特性(這對錶單輸入),但它確實innerHTML財產。只要確保你的return你想要的值是checkInputs(),而不是alert

例如:

function checkinputs() { 
    var A = parseInt(document.triangleform.input1.value), 
     B = parseInt(document.triangleform.input2.value), 
     C = parseInt(document.triangleform.input3.value), 
     result; 


    if (A == B && B == C) { result = "Equilateral"; } 
    if (A == B && B != C || B == C && A != B || C == A && A != B) { result ="Isosceles";} 
    if (A != B && B != C && C != A) { result = "Scalene!";} 

    // return result; 
    document.getElementById("Output").innerHTML = result; 
} 

然後在您的標記:

<script> 
     document.getElementById("Output").innerHTML = checkinputs(); 
</script> 

注意.innerHTML是區分大小寫的。

+0

因此,而不是它是'{警報( 「名稱」);}'這將是 '如果(A = B && B = C && C = A!!!){回報;}? ' '

\t \t \t \t
' –

+0

增加了一個例子,希望有所幫助。 –

+0

謝謝克里斯,我只是改變了你的功能(重新寫它自己學習),並改變了div: '

\t \t \t \t
' 仍然沒有變化?或者我錯過了什麼?它會是checkinputs(返回); –

-1
function checkinputs(){ 
    var A = parseInt(document.triangleform.input1.value); 
    var B = parseInt(document.triangleform.input2.value); 
    var C = parseInt(document.triangleform.input3.value); 

    if (A == B && B == C) { $('#output').html("<div>Equalateral</div>");} 
    if (A == B && B != C || B == C && A != B || C == A && A != B) {$('#output').html("<div>Isosceles</div>");} 
    if (A != B && B != C && C != A) {$('#output').html("<div>Scalene!</div>");} 
} 
+0

這將工作,但它會更有幫助,如果你能解釋爲什麼。 –

+0

嗨拉胡爾,謝謝你的迴應。如果可以的話,我會很感激:) –

+0

此外,這裏沒有jQuery要求。 –