我正在尝试使用 jQuery 构建一个随机团队创建者。我已经完成了函数的工作,它创建了随机团队,但现在我正试图将创建的团队的玩家放入按ul
. 我试过了,$('#team1 li').html( team );
但整个团队都显示在一个li
元素中。如何在一个li
元素中显示一个玩家?
这是HTML代码:
<form>
<div class="teamsNumber">How many teams do you want to create?<br>
<input type="text" name="teamsNumber"><br>
</div>
<br>
<div class="players">Players:<br>
<textarea rows="12" cols="20"></textarea>
</div>
<input type="submit" value="Submit"><br>
</form>
<div class="resultingTeams">
<h1>Team 1</h1>
<ul id="team1">
<li id="p1">player 1</li>
<li id="p2">player 2</li>
<li id="p3">player 3</li>
<li id="p4">player 4</li>
<li id="p5">player 5</li>
</ul>
</div>
这是jQuery代码:
$(document).ready(function () {
$("form").submit(function (event) {
event.preventDefault();
var playerNames = $(".players textarea").val();
var players = playerNames.split("\n");
var playersAmount = (players.length);
var teamsAmount = $(".teamsNumber input").val();
var mixedPlayers = [];
var minIndex = 0;
var maxIndex = playersAmount - 1;
for (i = 0; i < playersAmount; i++) {
var random = getRandom(minIndex, maxIndex);
while (mixedPlayers[random] !== undefined) {
random = getRandom(minIndex, maxIndex);
}
mixedPlayers[random] = players[i];
}
var breakpoint = Math.ceil(playersAmount / teamsAmount);
$('#resultingTeams').html("");
for (i = 0; i < teamsAmount; i++) {
var team = mixedPlayers.splice(0, breakpoint);
//$('.resultingTeams').append(" ");
console.log(team)
}
function getRandom(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
$('#team1 li').html(team);
});
})