2016-08-03 61 views
1

我有一個JS代碼來計算一個數字的倍數。我想在我的HTML文件中加入相同的內容。所以當我點擊「提交」按鈕時,它給了我答案。以下是我的代碼:Javascript代碼將被嵌入在HTML

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<meta author="Shruti Paliwal"> 
<title>Project Euler Solutions</title> 
<script language="javascript" type="text/javascript"> 
    function multiples() 
    { 
     var i; 
     sum=0; 
     for(i=1;i<1000;i++) 
     { 
      if(i%3 === 0 || i%5 === 0) 
       { 
        sum+=i; 
       } 

     } 

    } 
    document.getElementById('sum').value = sum; 
</script> 
</head> 
<body> 
<h3> 
<div id="header"> 
Project Euler 
<sub>.net</sub> 
</h3> 
</div> 
<br /> 
<br /> 
<p class="problem1" id="problem"> 
<b> 
Multiples of 3 and 5 
</b> 
<div class="boundary" id="boundary"> 
<div class="content"> 
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. 
<br /> 
Find the sum of all the multiples of 3 or 5 below 1000. 
<br /> 
<br /> 
<form> 
<input type="text" name="value"> 
<input type="button" onclick="sum()" value="Submit" /> 

</form> 
</div> 
</p> 
</div> 
</style> 
</body> 
</html> 

需要幫助才能在我的網頁上顯示此代碼的輸出。計算倍數的邏輯在JS控制檯上工作正常。但是,沒有得到如何納入2!

+4

沒有代碼 – brk

+6

您似乎沒有在這方面付出太多的努力。 ; P – Matt

+1

第37行是罪魁禍首 –

回答

1

你的代碼更改爲:

<p class="problem1" id="problem"> 
    <b> 
     Multiples of 3 and 5 
    </b> 
    <div class="boundary" id="boundary"> 
     <div class="content"> 
      If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. 
      <br /> 
      Find the sum of all the multiples of 3 or 5 below 1000. 
      <br /> 
      <br /> 
      <form> 
       <p>Enter number 1:</p><input type="text" id="txtNumber1"> 
       <br /> 
       <p>Enter number 2:</p><input type="text" id="txtNumber2"> 
       <br /> 
       <br /> 
       <input type="button" onclick="multiples()" value="Submit" /> 

       <input type="text" name="value" id="sum"> 


      </form> 
     </div> 

    </div> 
<script language="javascript" type="text/javascript"> 
    function multiples() { 
     ////Jquery 
     //var number1 = $("#txtNumber1").val(); 
     //var number2 = $("#txtNumber2").val(); 

     //Javascript 
     var number1 = document.getElementById('txtNumber1').value; 
     var number2 = document.getElementById('txtNumber2').value; 

     if (number1.trim() == '' || number2.trim() == '') 
     { 
      alert('please enter number1 and number2'); 
      return false; 
     } 

     var i; 
     sum = 0; 
     for (i = 1; i < 1000; i++) { 
      if (i % number1 === 0 || i % number2 === 0) { 
       sum += i; 
      } 

     } 
     document.getElementById('sum').value = sum; 
     return false; 
    } 
</script> 
+0

謝謝如此多!!!它現在工作完美!!非常感謝! –

+1

此解決方案的唯一問題是,在HTML端沒有ID總和。 –

+0

@Shruti:你歡迎撥打 –

0

的Javascript 101

<script> 
 
function myFunction() { 
 
    document.getElementById("demo").innerHTML = "YOU CLICKED ME!"; 
 
} 
 
</script>
<button onclick="myFunction()">Click me</button>

OR

function calculate() { 
 
    var myBox1 = document.getElementById('box1').value; 
 
    var myBox2 = document.getElementById('box2').value; 
 
    var result = document.getElementById('result'); 
 
    var myResult = myBox1 * myBox2; 
 
    result.innerHTML = myResult; 
 

 
}
<input id="box1" type="text" /> 
 
<input id="box2" type="text" /> 
 
<div id="result"></div> 
 
<input type="submit" onclick="calculate()"/>

+0

嗨,這是行不通的( –