简体   繁体   English

从函数返回多个值到表单

[英]Returning multiple values to a form from a function

I am new to JavaScript and am having problems returning multiple values back to a form. 我是JavaScript新手,在将多个值返回到表单时遇到问题。 Here is my code: 这是我的代码:

The form: 表格:

<form name="form1">

First option
<input type="text" size="20" name="OptA" rows="1">

Second option
<input type="text" size="20" name="OptB" rows="1">

<input type="button" name="calc" value="Calculate" onclick="Evaluate(document.form1.OptA, document.form1.OptB, document.form1.TheStr);">

<input type="text" size="5" name="TheStr" readonly><input type="text" size="5" name="TheSte" readonly>

</form>

The script: 剧本:

function Evaluate(OptA, OptB, TheStr)
{       
    var A_Count = 0;
    var B_Count = 0;
    var C_Count = 0;
    var D_Count = 0;
    var Str1 = 0;
    var Str2 = "";
    var Str3 = 0;
    var Str4 = "";
    var Char1 = "";
    var Char2 = "";
    var Char3 = "";
    var Char4 = "";
    var Total = 0;
    var TheOptions = OptA.value + OptB.value;
    var A1 = "";
        TheStr.value = "";
        TheOptions = TheOptions.toUpperCase();
        for (var i = 0; i < TheOptions.length; i++)
        {
                if (TheOptions.charAt(i) == 'A')
                {       A_Count++;      }
                else if (TheOptions.charAt(i) == 'B')
                {       B_Count++;      }
        }
        Str1 = "" + A_Count+B_Count;
        Str2 = "";
        while ((Str1.length > 2) & (Count < 20))
        {
                Str2 = "";
                for (var i = 0; i < Str1.length - 1; i++)
                {
                        Char2 = Str1.charAt(eval(i+1));
                        Str2 = Str2 + (parseInt(Str1.charAt(i)) + parseInt(Str1.charAt(eval(i+1))));
                }
                Count++;
                Str1 = Str2;
        }
        if (Count > 19)
        {
                TheStr.value = '0%';
        }
        else
        {
                TheStr.value = parseInt(Str2,10) + '%';
        }

    for (var i = 0; i < TheOptions.length; i++)
        {
                if (TheOptions.charAt(i) == 'C')
                {       C_Count++;      }
                else if (TheOptions.charAt(i) == 'D')
                {       D_Count++;      }
        }
        Str3 = "" + C_Count+D_Count;
        Str4 = "";
        while ((Str3.length > 2) & (Count < 20))
        {
                Str4 = "";
                for (var i = 0; i < Str3.length - 1; i++)
                {
                        Char4 = Str3.charAt(eval(i+1));
                        Str4 = Str4 + (parseInt(Str3.charAt(i)) + parseInt(Str3.charAt(eval(i+1))));
                }
                Count++;
                Str3 = Str4;
        }
        if (Count > 19)
        {
                TheSte.value = '0%';
        }
        else
        {
                TheSte.value = parseInt(Str2,10) + '%';
        }

    TheStr=(TheStr.value, TheSte.value)

}

For some reason it is only returning a value to the first input box, the second one remains blank. 由于某种原因,它只向第一个输入框返回一个值,第二个输入框保持空白。 I guess my problem is figuring out how to return more than one value and then how to parse it into the two input boxes. 我想我的问题是弄清楚如何返回多个值,然后将其解析为两个输入框。 Any suggestions? 有什么建议么?

Both inputs are named "TheStr". 两个输入都被命名为“ TheStr”。 You need to name the second input "TheSte" if I understand correctly and also declare it in your Evaluate function. 如果我理解正确,则需要将第二个输入命名为“ TheSte”,并在您的Evaluate函数中声明它。

Edit: Also, the final instruction 编辑:另外,最后一条指令

TheStr=(TheStr.value, TheSte.value)

has no effect. 没有效果。 In your function TheStr is a text input. 在您的函数中,TheStr是文本输入。 You change its value by setting the value property 您可以通过设置value属性来更改其值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM