我有一个奇怪的问题,我的函数正在正常工作(一个 jQuery 抽奖生成器),但除非我按下浏览器上的后退按钮,否则它不会显示输出。代码是:
$(document).ready(function() {
$("input[name=Submit]").click(function() {
var x = $("input[name=playerNumber]").val();
var y = $("input[name=teamNumber]").val();
var playerArray = new Array(x);
var teamArray = new Array(y);
var areaToAddTo = $('#playerArea');
for (var i = 0; i < x; i++) {
playerArray[i] = prompt("What is the name of player "+(i+1)+"?");
}
for (var j = 0; j < y; j++) {
teamArray[j] = prompt("What is the name of team "+(j+1)+"?");
}
var teamsToLeaveOut = teamArray.length % playerArray.length;
var count = 0;
var randomNumber;
while (count < teamsToLeaveOut) {
randomNumber = Math.floor(Math.random()*(teamArray.length));
teamArray.splice(randomNumber, 1);
count++;
}
count = teamArray.length;
var spliceTeam;
var l;
while (count > 0) {
for (i = 0; i < playerArray.length; i++) {
randomNumber = Math.floor(Math.random()*teamArray.length);
spliceTeam = teamArray[randomNumber];
l += ('<h3>'+playerArray[i]+'</h3>');
l += ('<p>'+spliceTeam+'</p>');
teamArray.splice(randomNumber,1);
count--;
}
}
if (1 == 1) {
areaToAddTo.append(l);
}
});
});
在运行该函数时,屏幕上没有写入任何内容,浏览器 url 为:
/sweepstake.html?playerNumber=2&teamNumber=5&Submit=Submit
按下后退按钮时,输出应为正常,浏览器 url 为:
/sweepstake.html
对于如何解决这个问题,有任何的建议吗?
谢谢。