从函数返回多个值到表单

时间:2013-01-06 20:21:14

标签: javascript forms

我是JavaScript新手,在将多个值返回给表单时遇到问题。这是我的代码:

表格:

<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>

剧本:

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)

}

由于某种原因,它只返回第一个输入框的值,第二个输入框保持空白。我想我的问题是弄清楚如何返回多个值,然后如何将其解析为两个输入框。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

两个输入都命名为“TheStr”。如果我理解正确,你需要命名第二个输入“TheSte”,并在你的Evaluate函数中声明它。

编辑:此外,最后的指示

TheStr=(TheStr.value, TheSte.value)

无效。在您的函数中,TheStr是一个文本输入。您可以通过设置值属性

来更改其值