我想将一些值存储在HTML中的数组中。这些值来自Python中的随机数生成器{{player.a1s1}}。这已经得到了解决。基本上,当每次点击按钮时#34; mm1a" ,玩家将在表单中看到另外一个按钮。将记录点击次数。我希望按钮上显示的随机数{{player.a1s1}}作为数组存储在JS中。我的代码无法将值存储到数组中。
HTML
<div class="container1">
<p>
{{ player.text1a }} # this will get the text from a python variable.
</p>
<form id="frm1">
<button class="toAdd">{{player.a1s1}}</button>
<button class="toAdd">{{player.a1s1}}</button>
<button class="toAdd">{{player.a1s1}}</button>
<button class="toAdd">{{player.a1s1}}</button>
<button class="toAdd">{{player.a1s1}}</button>
<button class="toAdd">{{player.a1s1}}</button>
</form>
</div>
<div><button class="mm1a" type="button">{{ player.optiona1 }}</button>
# this will get the innerhtml from another python variable.
</div>
<p>
samplemma1<input id="id_samplemma1" name="samplemma1" value="0"></input>
</p>
在javascript中
$('.mm1a').on('click',function(){
$('.toAdd:eq('+count+')').show();
count++;
hello();
getarraya();
});
var clicks = 0; # to count how many clickes the players did
function hello() {
clicks += 1;
document.getElementById("id_clicksmma1").innerHTML = clicks;
document.getElementById("id_clicksmma1").value = clicks;
};
# below part does not work
var myarraya = [];
var c = 0;
function getarraya() {
c += 1;
myarraya.push(document.forms["frm1"].elements[c].innerHTML);
document.getElementById("id_clicksmma1").innerHTML = myarraya;
document.getElementById("id_clicksmma1").value = myarraya;
};
答案 0 :(得分:0)
jquery&#34; on&#34;必须在document.ready和变量myarraya里面 并将其移至顶部。