gpt4 book ai didi

javascript - 如何将变量从 JavaScript 函数发送到文本框?

转载 作者:行者123 更新时间:2023-11-29 10:49:41 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Set the value of a input field with 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>

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com