可能重复:
使用 javascript 设置输入字段的值
好的,所以我试图让我的函数结果返回输入框(这是计算器的一部分)。一个例子是:我在计算器中输入 1,然后它执行 1+2+3+4+5+6+7+8+9+10 并通过下面的函数将 55 输出到警报消息框中。我试图让结果显示在框内而不是警报框内。
下面我提供了以下代码:“L”函数、“输入”框和控制该函数的按钮 onClick。
我已经在这个问题上待了大约一个星期,试图弄清楚。如果您需要计算器中的更多代码,我可以提供。
function Loop(input)
{
var num = parseInt(input) + 10;
var i=0;
var sum=0;
while(i < num)
{
sum=sum+i;
i++;
}
alert(sum);
}
<input type="button" value=" L " name="L" onClick="Loop(this.form.display.value)">
<form><input name="display" id="input" size=25><form>
这是其余的代码,因为我在下面得到的两个答案不起作用(尽管感谢您尝试解决!)
<html>
<head>
<title>Assignment 2</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Functions
function addChar(input, character)
{
if(input.value == null || input.value == "0")
input.value = character;
else
input.value += character;
}
function changeSign(input)
{
if(input.value.substring(0, 1) == "-")
input.value = input.value.substring(1, input.value.length);
else
input.value = "-" + input.value;
}
function compute(form)
{
form.display.value = eval(form.display.value);
}
function Loop(input)
{
var num = parseInt(input) + 10;
var i=0;
var sum=0;
while(i < num)
{
sum=sum+i;
i++;
}
document.getElementById("input").value=sum;
}
// End -->
</SCRIPT>
</head>
<body>
<div align="center">
<span style="font-weight:bold" size="20">Calculator</span>
<br>
<!-- Prints my name -->
<form name="MyName" id="form1" style="font-weight:bold" size="20">
<script>
document.write("Mallery, Cody");
</script>
</form>
<!-- functions -->
<!-- The Calculator! -->
<center><form>
<table border=4>
<tr>
<td>
<input name="display" id="input" size=25>
<br>
</td>
</tr>
<tr>
<td>
<input type="button" value=" 7 " onClick="addChar(this.form.display, '7')">
<input type="button" value=" 8 " onClick="addChar(this.form.display, '8')">
<input type="button" value=" 9 " onClick="addChar(this.form.display, '9')">
<input type="button" value=" / " onClick="addChar(this.form.display, '/')">
<br>
<input type="button" value=" 4 " onClick="addChar(this.form.display, '4')">
<input type="button" value=" 5 " onClick="addChar(this.form.display, '5')">
<input type="button" value=" 6 " onClick="addChar(this.form.display, '6')">
<input type="button" value=" * " onClick="addChar(this.form.display, '*')">
<br>
<input type="button" value=" 1 " onClick="addChar(this.form.display, '1')">
<input type="button" value=" 2 " onClick="addChar(this.form.display, '2')">
<input type="button" value=" 3 " onClick="addChar(this.form.display, '3')">
<input type="button" value=" - " onClick="addChar(this.form.display, '-')">
<br>
<input type="button" value=" 0 " onClick="addChar(this.form.display, '0')">
<input type="button" value=" N " onClick="changeSign(this.form.display)">
<input type="button" value=" + " onClick="addChar(this.form.display, '+')">
<input type="button" value=" C " onClick="this.form.display.value = 0 ">
<br>
<input type="button" value=" L " name="L" onClick="Loop(this.form.display.value)"
title="If the L button is pressed, the digit present in the results box will be looped through and added up to the 'digit plus 10'.For example: After the calculator has been reset. The user can press the 1 button, then the L button 55 should be displayed in the calculator 1 + 10 = 11, therefore start with 1 and loop until less than 11 adding all of the numbers 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55">
<input type="button" value=" = " name="enter" onClick="compute(this.form)">
</td>
</tr>
</table>