【问题标题】:How do I send a variable from a JavaScript function to a text box? [duplicate]如何将变量从 JavaScript 函数发送到文本框? [复制]
【发布时间】:2012-10-04 12:15:03
【问题描述】:

可能重复:
Set the value of a input field with javascript

好的,所以我试图让我的函数的结果返回输入框(这是计算器的一部分)。一个例子是:我在计算器中输入 1,然后它执行 1+2+3+4+5+6+7+8+9+10 并通过下面的函数将 55 输出到警报消息框中。我试图让结果显示在框内而不是警告框内。

下面我提供了以下代码: “L”函数、“输入”框和控制该函数的 Button 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>

【问题讨论】:

  • ypu 可以制作 jsfiddle 并告诉哪些是行不通的吗?
  • getElementbyId 应该是 getElementById。查看控制台的错误消息!不要只是盲目地复制和粘贴代码,调查一下!
  • Shoot,我在帖子中发布了错误的代码。我应该咳出那个错误!对不起。

标签: javascript function variables object


【解决方案1】:

把它放在你的 JS 函数中而不是你的警报中

document.getElementById("input").value=sum;

【讨论】:

  • getElementbyId 应该是 getElementById
  • 是的。你是对的。其实我是直接写在这里的。但它有效
  • 大写 B 而不是小写 b
  • 那行不通。不过谢谢!我也使用了大写 B。
  • 是的,我已经更新了我的代码。也感谢你们
【解决方案2】:

我假设您输入的文本字段是用 type="text" 正确编写的。

您可以为元素分配一个 id,然后使用 javascript 设置它的值。 使用document.getElementbyId("&lt;Id&gt;")获取对象

然后设置值document.getElementbyId("&lt;Id&gt;").value=&lt;value&gt;

【讨论】:

  • 我已经编辑了 OP,因为我认为为了帮助我,你们需要更多关于我实际尝试做的信息。
【解决方案3】:

试试

document.forms[0].display.value=sum;

【讨论】:

  • 这也没用,不过还是谢谢。
  • 在你的代码中你有“form”而不是“forms”。
  • 请在重试之前删除 getElementByID,它包含一个错误,会在此时停止脚本。
  • 我修好了,它有效;感谢您的帮助!
【解决方案4】:

您只需要进行 3 次更改。然后您可以在任何文本输入上使用相同的功能,就像现在一样。

首先你要将元素本身传递给函数

function Loop(input)
{

变成

function Loop(inputElement)
{
  var input = inputElement.value;

接下来,将您的警报输出语句更改为:

inputElement.value = sum;

最后,更改您的 html(删除 '.value')

onClick="Loop(this.form.display)"

【讨论】:

【解决方案5】:

您没有正确检索输入值。 你可以这样做:

<input name="display" id="input1" size="25">
<form>
    <input type="button" value="   L    " name="L" onClick="Loop()">
<form>
<script type="text/javascript">

function Loop()
{
    var input = document.getElementById("input1").value;
    var num = parseInt(input) + 10;
    var sum=0;

    for(var i=0; i<num; i++)
    {
        sum = sum+i;
    }

    document.getElementById("input1").value = sum;
}

</script>

我还将 while 替换为 for,因为在这种情况下,我认为它更易于阅读。

【讨论】:

  • 它工作正常我试图让输出回到“输入”元素而不是警报消息。不过感谢您的帮助! :)
【解决方案6】:
<input type="button" value="   L    " name="L" onClick="Loop(this.form.display.value)">

this 这里指的是&lt;input&gt;,它没有表单,你的document 有表单,所以: document.forms[0].display.value 会给你你所需要的;


您的更新:
http://jsfiddle.net/jNqEv/3/

function Loop(input)
{
     var num = parseInt(input) + 10;
     var i=0;
     var sum=0;

    while(i < num)
        {
            sum=sum+i;
            i++;
        }
    // NOTE: it's not a good idea to give an item `input` ID;
    document.getElementById("input").value=sum; // instead of document.getElementbyId("input").value=sum; 
    // what's this for?
    // document.form[0].display.value=sum; 
}​

【讨论】:

    猜你喜欢
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多