我试图增加从DOM获取的值
该值卡在“1”中;
我做错了什么?
HTML
<div class="col-md-1">
<button class="btn btn-success" id="btnAddFlak">+ Flak</button>
</div>
<div class="well" id="flakDiv"></div>
JS
var flakNr = 1;
//Create flak
$('#btnAddFlak').on('click', function () {
//Set flakNr
var flakNr = $('.flakMiddle').last().val();
console.log(flakNr);
//Increase flakNr
flakNr++;
console.log(flakNr);
//Flak HTML setup
var flak = $('<div class="flak nopadding"><div class="flakSideUp nopadding"></div><div class="flakMiddle">Flak <b>' + flakNr + '</b></div><div class="flakSideDown nopadding"></div></div><br>');
//Insert flak to flakDiv
$('#flakDiv').append(flak);
});
答案 0 :(得分:3)
答案 1 :(得分:0)
我建议也许你可以让另一个变量增加如下:
var flakNr = 1;
var ctr= 0; // or any number you want
//Create flak
$('#btnAddFlak').on('click', function () {
//Set flakNr
var flakNr = $('.flakMiddle').last().val();
console.log(flakNr);
//Increase flakNr with the value ctr
flakNr+=ctr;
// increment ctr
ctr++;
console.log(flakNr);
//Flak HTML setup
var flak = $('<div class="flak nopadding"><div class="flakSideUp nopadding"></div><div class="flakMiddle">Flak <b>' + flakNr + '</b></div><div class="flakSideDown nopadding"></div></div><br>');
//Insert flak to flakDiv
$('#flakDiv').append(flak);
});
这就是你的好去处!快乐的编码..