Estou criando uma página em que existem vários campos select (14 no total). Todos eles têm as mesmas opções. Minha ideia era preencher esses campos dinamicamente, sem precisar fazer direto no html. Criei um json com as opções e tentei carregar com um arquivo .js. Mas no fim das contas ainda fiquei com um html grande e não está preenchendo o select. abaixo seguem os códigos que talvez facilitem a explicar a minha dúvida
html
<h5>Sábado - Classificação</h5>
<div class="form-group">
<label for="drive" class="bet-text-option">Pole</label>
<select name="drivers" id="drivers" class="form-control drivers-options">
</select>
<label for="drive" class="bet-text-option">2º</label>
<select name="drivers2" id="drivers2" class="form-control drivers-options">
</select>
<label for="drive" class="bet-text-option">3º</label>
<select name="drivers3" id="drivers3" class="form-control drivers-options">
</select>
<label for="drive" class="bet-text-option">Q3: 8º</label>
<select name="drivers4" id="drivers4" class="form-control drivers-options">
</select>
<label for="drive" class="bet-text-option">Q2: 13º</label>
<select name="drivers5" id="drivers5" class="form-control drivers-options">
</select>
<label for="drive" class="bet-text-option">Q1: 17º</label>
<select name="drivers6" id="drivers6" class="form-control drivers-options">
</select>
</div>
<h5>Domingo - Corrida</h5>
<div class="form-group" class="col">
<label for="drive" class="bet-text-option">Vencedor</label>
<select name="drivers7" id="drivers7" class="form-control drivers-options">
</select>
<label for="drive" class="bet-text-option">2º</label>
<select name="drivers8" id="drivers8" class="form-control drivers-options">
</select>
<label for="drive" class="bet-text-option">3º</label>
<select name="drivers9" id="drivers9" class="form-control drivers-options">
</select>
<label for="drive" class="bet-text-option">4º</label>
<select name="drivers10" id="drivers10" class="form-control drivers-options">
</select>
<label for="drive" class="bet-text-option">5º</label>
<select name="drivers11" id="drivers11" class="form-control drivers-options">
</select>
<label for="drive" class="bet-text-option">Posição aleatória: 7º</label>
<select name="drivers12" id="drivers12" class="form-control drivers-options">
</select>
<label for="drive" class="bet-text-option">Primeiro Abandono</label>
<select name="drivers13" id="drivers13" class="form-control drivers-options">
</select>
<label for="drive" class="bet-text-option">Volta mais rápida</label>
<select name="drivers14" id="drivers14" class="form-control drivers-options">
</select>
<a href="#" class="btn btn-primary bet-btn">Apostar</a>
</form>
JS
var select = $('#drivers');
$.getJSON('drivers.JSON', function(data) {
select.html('');
for(var i = 0; i < data['drivers'].length; i++) {
select.append('<option id="' + data['drivers'][i]['id'] + '">' + data['drivers'][i]['name'] + '</option>');
}
})
var select2 = $('#drivers2');
$.getJSON('drivers.json', function(data) {
select2.html('');
for(var i = 0; i < data['drivers'].length; i++) {
select2.append('<option id="' + data['drivers'][i]['id'] + '">' + data['drivers'][i]['name'] + '</option>');
}
})
var select3 = $('#drivers3');
$.getJSON('drivers.json', function(data) {
select3.html('');
for(var i = 0; i < data['drivers'].length; i++) {
select3.append('<option id="' + data['drivers'][i]['id'] + '">' + data['drivers'][i]['name'] + '</option>');
}
})
var select4 = $('#drivers4');
$.getJSON('drivers.json', function(data) {
select4.html('');
Até o select14