在我的情況下,我想用加號或減號將加號或減號按鈕計入文本框中。我能行。但我有錯誤。有相同設計但ID不同的多行(我的意思是使用自動增量ID)。當我點擊頂部或中間行的加號按鈕,但它不適用於它自己的行。它總是在最後一行工作。如何從多個ID獲取特定ID?
這裏是我的jQuery代碼的HTML。當點擊右上角的添加按鈕時,此代碼工作。這不是問題。
<input id="qty1" type="text" class="form-control dateadd" style="border- radius:0px;" name="count_date[]" value="0">
這是我處理JQuery代碼。
var test;
$('.dateadd').each(function() {
var test = this.id;
console.log("ID is",test, "Hello, world!");
});
$(document).ready(function(){
$('.plus').click(function() {
var counter = $(test).val();
counter++ ;
$(test).val(counter);
});
});
$(document).ready(function(){
$('.minus').click(function() {
var counter = $(test).val();
counter-- ;
$(test).val(counter);
});
});
這是我行HTML代碼
<div class="col-sm-5" style="padding-right:0;">
<div class="input-group">
<span class="input-group-btn">
<a class="btn btn-default plus" type="button" style="background-color:#7ec1cb;color:#fff;border-radius:0px;">+</a>
<a class="btn btn-default minus" style="color:#7ec1cb;border-radius:0px;">-</a>
</span>
<input type="text" class="form-control qty" style="border-radius:0px;" name="count_date[]" value="0">
</div>
</div>
這是我行jQuery的添加+ HTML代碼
var perDayHtml = '<div class="col-sm-5" style="padding-right:0;">'+
'<div class="input-group">'+
'<span class="input-group-btn">'+
'<a class="btn btn-default plus" type="button" style="background-color:#7ec1cb;color:#fff;border-radius:0px;">+</a>'+
'<a class="btn btn-default minus" style="color:#7ec1cb;border-radius:0px;">-</a>'+
'</span>'+
'<input id="qty1" type="text" class="form-control dateadd" style="border-radius:0px;" name="count_date[]" value="0">'+
'</div>'+
'</div>'
$("#optionBox").on('click', '.addPerDay', function(){
$('.remove').prop('disabled', false);
if (rowNo<maxi && rowNo<minusmaxi) {
rowNo++;
plan++;
$(".perDay:last").after(perDayHtml.replace(/qty1/g,"qty" + rowNo));
添加HTML代碼PLZ。 –
你的html是矛盾的 - 一個說輸入類是qty,另一個說它是dateadd - 哪一個是它的呢? – Pete