gpt4 book ai didi

javascript - 将 Javascript 值添加到隐藏输入

转载 作者:行者123 更新时间:2023-11-30 11:30:26 25 4
gpt4 key购买 nike

我正在尝试将 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 并隐藏输入类型。

最佳答案

你可以简单地这样做:

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

document.getElementById("pickednumbers").setAttribute("value",JSON.stringify(pickednumbers));
<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 - 将 Javascript 值添加到隐藏输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46432077/

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