我有一个脚本在选择列表选项中显示一个数字,当用户检查一个特定值时,它会显示一个数字,表示他可以支付账单的次数。
var tabelaParcelas = [];
tabelaParcelas[1] = 2;
tabelaParcelas[2] = 3;
tabelaParcelas[3] = 4;
tabelaParcelas[4] = 6;
tabelaParcelas[5] = 8;
tabelaParcelas[6] = 10;
tabelaParcelas[7] = 12;
$(document).ready(function(){
update();
});
$('input[type=checkbox]').click(function(){
update();
})
function update(){
var amount2 = 0;
var options = 0;
$('#instNum2 option').remove();
$('.check_list').each(function () {
if (this.checked) {
amount2 += Number($(this).val());
options ++;
}
$("input[name=amount]").val("R$"+amount2.toFixed(2));
});
if (options > 7) options = 7;
var i = 0;
if (options > 0) {
while (tabelaParcelas[options] != i) {
if (i != 0) {
$('#instNum2')
.append($("<option></option>")
.attr("value",i+1)
.text((i+1)+"x"));
}
i++;
}
}
}
<table class="table table-striped table-bordered table-hover">
<thead>
<th style="text-align: center;">Selecione</th>
<th style="text-align: center;">Nome</th>
<th style="text-align: center;">Situação</th>
<th style="text-align: center;">Parcela</th>
<th style="text-align: center;">Vencimento</th>
<th style="text-align: center;">Valor Parcela</th>
<th style="text-align: center;">Valor Devido</th>
<th style="text-align: center;">Nome Responsável</th>
</thead>
<tbody>
<?php
foreach ($result as $value) {
echo '<tr style="text-align: center;">
<td><input style="cursor: pointer;" type="checkbox" value="'. $value['VALORDEVIDO'] .'" class="check_list"></input>
<input style="display: none;" type="checkbox" checked="true" value="'. $value['CODIGOPARCELA'] .'" name="numControle"></td>
<td>'. utf8_encode($value['NOME']) .'</td>
<td>'. $value['SITUACAO'] .'</td>
<td>'. $value['PARCELA'] .'</td>
<td>'. $value['VENCIMENTO'] .'</td>
<td>'. $value['VALORPARCELA'] .'</td>
<td>'. $value['VALORDEVIDO'] .'</td>
<td>'. utf8_encode($value['NOMERESPONSAVEL']) .'</td>
</tr>';
}
?>
</tbody>
</table>
<select class="form-control" id="instNum2" name="instNum"></select>
问题是,脚本一直显示5x,7x,9x和11x,而且1x甚至没有显示,我需要在第1位理解为什么应该出现的那个出现...
有人可以帮助我吗?
答案 0 :(得分:0)
请尝试以下代码:
var tabelaParcelas = [1, 2, 3, 4, 6, 8, 10, 12];
$(document).ready(function() {
update();
});
$('input[type=checkbox]').click(function() {
update();
})
function update() {
var list = $('#instNum2'); // use selector only once whenever possible for better performance
// clear any existing options from the dropdown list
list.html("")
// count checked boxes, start from 1 to always include 1 option
var checked_boxes = 1
$(".check_list").each(function() {
if (this.checked) {
checked_boxes++;
}
});
// append options to the select list
for (var i = 0; i < checked_boxes; i++) {
// break the loop to stop creating empty options
if(i == tabelaParcelas.length){
break;
}
var option = $("<option>", {
value: tabelaParcelas[i],
"text": tabelaParcelas[i]
});
list.append(option);
}
}