将Javascript值添加到隐藏输入

时间:2017-09-26 17:00:19

标签: javascript jquery html variables

我正在尝试将JavaScript变量添加到隐藏的输入字段中。我该怎么做?

HTML

<button type="button" class="submitFormOnClick btn btn-warning pull-right"
        style="margin-bottom:15px; margin-top:15px;" id="bet">Bet
</button>

<input type="hidden" name="controller" value="keno">
<input type="hidden" name="task" id="task" value="">
<input type="hidden" name="pickednumbers" id="pickednumbers">

的JavaScript

<script type="text/javascript">
    var inc = 0;
    var pickednumbers = [];
    function myFunction(elmnt, color, id)
    {
        if(pickednumbers.length >= 20)
        {
            return false;
        }

        if (pickednumbers.indexOf(id) === -1)
        {
            pickednumbers.push(id);

            elmnt.style.background = color;
            inc = inc + 1;
            if(inc >= 20)
            {
                document.getElementById('onColor').setAttribute("style", "background-color:green");
            }
        }
        else
        {
            return false;
        }
    }
</script>

我需要什么: 我想要的是让变量pickednumbers与隐藏输入类型一起发送。

1 个答案:

答案 0 :(得分:3)

您可以这样做:

&#13;
&#13;
var inc = 0;
var pickednumbers = [2,3,4,8];

document.getElementById("pickednumbers").setAttribute("value",JSON.stringify(pickednumbers));
&#13;
<button type="button" class="submitFormOnClick btn btn-warning pull-right" style="margin-bottom:15px;margin-top:15px;" id="bet">Bet
</button>

<input type="hidden" name="controller" value="keno">
<input type="hidden" name="task" id="task" value="">
<input type="hidden" name="pickednumbers" id="pickednumbers">
&#13;
&#13;
&#13;