将所有输出放在 JavaScript 的 while 循环中的特定文本框中

Putting all the output in a specific textbox in a while loop in JavaScript

所以我只想在这里发生的是如何将所有给定的输出放入答案文本框中。

更新

<html>
 <body>
  <script type="text/javascript">
      function myFunction() {
          var x = parseInt(document.f1.n1.value);
          var y = parseInt(document.f1.n2.value);
          if(x>=y) {
              document.write("Invalid");
          }
          while (x<y)
          {
              document.write(x)
              x=x+1;
          }
      }
  </script>
  <form name=f1 onsubmit="return false">
   First no. <input type="text" name="n1">
   Second no. <input type="text" name="n2">
   Answer: <input type="text" name="ans" disabled>
   <input type="submit" onClick="myFunction()">
  </form>
 </body>
</html>

提前致谢

你可以这样做:

给答案输入一个id:

<input id="answerTextBox" type="text" name="ans" disabled>

while 循环后填充答案文本框:

while (x>y)
{
document.getElementById("answerTextBox").value += ("" + x);
x=x+1;
}

这会将 x 和 y 放入您的答案文本框中。

更新******

<html>
 <body>
  <script type="text/javascript">
      function myFunction() {
          document.getElementById("answerTextBox").value = "";
          var x = parseInt(document.getElementById("n1").value);
          var y = parseInt(document.getElementById("n2").value);
          if(x >= y)
          {
              document.getElementById("answerTextBox").value = "Invalid Input";
          }
          while (x < y)
          {
              document.getElementById("answerTextBox").value += ("" + x);
              x=x+1;
          }
      }
  </script>
   First no. <input type="text" name="n1" id="n1">
   Second no. <input type="text" name="n2" id="n2">
   Answer: <input id="answerTextBox" type="text" name="ans" readonly>
   <input type="button" onClick="myFunction()">
  </form>
 </body>
</html>

更新指数************************

<html>
 <body>
  <script type="text/javascript">
      function myFunction() {
          document.getElementById("answerTextBox").value = "";
          var x = parseInt(document.getElementById("n1").value);
          var y = x;
          var answer = 1;
          for(var i = 0; i < y; i++)
          {
              answer = answer * x;
          }
          document.getElementById("answerTextBox").value = answer;
      }
  </script>
   Enter Number: <input type="text" name="n1" id="n1">
   Answer: <input id="answerTextBox" type="text" name="ans" readonly>
   <input type="button" value="Submit" onClick="myFunction()">
  </form>
 </body>
</html>

更新添加指数*****************

<html>
 <body>
  <script type="text/javascript">
      function myFunction() {
          document.getElementById("answerTextBox").value = "";
          var x = parseInt(document.getElementById("n1").value);
          var y = parseInt(document.getElementById("n2").value);
          var answer = 0;
          for(var i = 0; i <= y; i++)
          {
              answer = answer + Math.pow(x, i);
          }
          document.getElementById("answerTextBox").value = answer;
      }
  </script>
   Enter Base Number: <input type="text" name="n1" id="n1">
   Enter Highest Exponent: <input type="text" name="n2" id="n2">
   Answer: <input id="answerTextBox" type="text" name="ans" readonly>
   <input type="button" value="Submit" onClick="myFunction()">
  </form>
 </body>
</html>
<html>
<body>
The number inserted in the textbox is the number of exponent it will generate. The sample is 3. 3X3X3 is 27. <br>
<script type="text/javascript">
function myFunction() {
var x = document.f1.n1.value;
while(x<27)
{
x=x*3;
}
document.write(x)
}
</script>
<form name="f1" onsubmit="return false">
First no. <input type="text"name="n1" value=3 disabled>
<input type="submit" onClick="myFunction()">
</form>
</body>
</html>

更新:任意数的指数。