使用.each()的jquery增量号

ton*_*lfx 1 checkbox jquery increment

如何使用.each()函数增加id.

$("input:checked").each(function(){
var counter = $(this).length();
var id_bookslot = data; //<-- increment id +1
var treatment_type = $(this).closest("div").attr("id");
var id_treatment = $(this).attr("class");
$.post("include/get_booking.php?insert", {id_bookslot: id_bookslot,id_treatment:id_treatment,treatment_type:treatment_type});
});
Run Code Online (Sandbox Code Playgroud)

比方说,有3个复选框被选中!所以id将递增到3(1,2,3).

我忘了提及var id_bookslot = data.data这是我从数据库中检索的id.假设它从1234开始.每次.each()生成,它将增加1. 1234,1235,1236

Mac*_*ony 6

每个()方法,可以使用该元素的索引.这可能是实现这一目标的最佳方式.

$("input:checked").each(function( index ){
    var id_bookslot = index + 1; //<-- increment id +1
    var treatment_type = $(this).closest("div").attr("id");
    var id_treatment = $(this).attr("class");
    $.post("include/get_booking.php?insert", {id_bookslot: id_bookslot,id_treatment:id_treatment,treatment_type:treatment_type});
});
Run Code Online (Sandbox Code Playgroud)

我添加了+1,因为索引是0索引,你似乎希望它从1开始.