我现在有一个页面,上面有一组下拉列表(4),这些下拉列表用于从列表中选择数据并提交。它用于为锦标赛选择球队,所以目前我只能创建一个有 4 支球队的锦标赛。
我想更改它,以便页面仅显示一个下拉菜单Enter a number of teams
,例如用户选择 4/8/16/32。根据用户选择的数字,页面应该生成这个数量的下拉菜单。
我很难做到这一点,无论用户选择什么数字,页面都需要呈现该数量的下拉菜单并相应地命名它们,即#team1, #team2, #team3
.
任何想法如何做到这一点?
这是我目前使用 Jade 模板的下拉列表,如果使用 Jade 有任何混淆,请见谅,但很容易理解它是什么:
div.newMatch
form.form-horizontal(action='/tournament', id="tournamentForm")
div.row
label.control-label(for="tournamentName") Tournament Name:
div.controls
input#tournamentName.input-mysize(type="text")
div.row
label.control-label(for="team1") Team 1:
div.controls
select#team1(style='width: 160px;')
include teamsDropDown
div.row
label.control-label(for="team2") Team 2:
div.controls
select#team2(style='width: 160px;')
include teamsDropDown
div.row
label.control-label(for="team3") Team 3:
div.controls
select#team3(style='width: 160px;')
include teamsDropDown
div.row
label.control-label(for="team4") Team 4:
div.controls
select#team4(style='width: 160px;')
include teamsDropDown
我试过的:
div.row
label.control-label(for="tournamentName") Tournament Name:
div.controls
input#tournamentName.input-mysize(type="text")
div.row
label.control-label(for="nTeamSelector") Tournament Size:
div.controls
select#nTeamSelector(style='width: 160px;')
option Please select...
option 2
option 4
option 8
option 16
option 32
div.row.hidden#rowTemplate
label.control-label(for="team") Team:
div.controls
select#team1(style='width: 160px;')
include teamsDropDown
div.row#rowContainer
script(type='text/javascript')
$("#nTeamSelector").change(function(){
var nTeams = $("#nTeamSelector").val(); // Get the value of the team selector that you use
$("#rowContainer").empty() // Delete all existing dropdowns
for (var i=0; i<nTeams; i++){
var newRow = $("#rowTemplate").clone(); // Exact copy of the element generated by Jade
/*
* here you should change all the little fields that
* make the dropdowns different. For example:
*/
newRow.children(".control-label").setText("Team "+(i+1)+":");
$("#rowContainer").append(newRow);
}
});