jquery 使用 .each() 递增数字

发布于 2024-11-02 15:09:19 字数 568 浏览 5 评论 0原文

如何使用 .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});
});

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

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

how do i increment id using .each() function.

$("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});
});

let say, there are 3 checkboxes are checked! so the id will be incrementing until 3(1,2,3).

i forgot to mention var id_bookslot = data. data which is an id that i retreive from database. let say it starts with 1234. and everytime .each() generate, it will increment by 1. 1234, 1235, 1236

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

带上头具痛哭 2024-11-09 15:09:19

each() 方法允许您使用元素的索引。这可能是实现这一目标的最佳方法。

$("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});
});

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

The each() method allows you to use the index of the element. That's likely the best way to accomplish this.

$("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});
});

I added +1 since the index is a 0 index and you seem to want it to start at 1.

网名女生简单气质 2024-11-09 15:09:19

如果您的目标是为每个复选框执行post,并提供索引或平滑,each 为您提供一个可以使用的索引(另外,避免重复编写 $(this) ,这是浪费的):

$("input:checked").each(function(index) {
  var $this = $(this);
  var id_bookslot = data + index + 1;          // <== Using the index here
  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
    }
  );
});

另外请注意,$(this).length 将始终为 1,但无论如何您都没有使用 counter 变量,所以我只是将其删除。如果您使用它但只是没有引用代码,请执行以下操作:

var checked = $("input:checked");
checked.each(function(index) {
  var $this = $(this);
  var id_bookslot = data + index + 1;          // <== Using the index here
  var treatment_type = $this.closest("div").attr("id");
  var id_treatment = $this.attr("class");
  $.post("include/get_booking.php?insert", {
      id_bookslot:    index,
      id_treatment:   id_treatment,
      treatment_type: treatment_type
    }
  );
});

...并使用 checked.length 作为您的 counter 变量。

If your goal is to do a post for each checkbox, and to give an index or smoething, each gives you an index you can use (also, avoid writing $(this) repeatedly, it's wasteful):

$("input:checked").each(function(index) {
  var $this = $(this);
  var id_bookslot = data + index + 1;          // <== Using the index here
  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
    }
  );
});

Also note that $(this).length will always be 1, but you weren't using your counter variable anyway, so I just removed it. If you use it but just didn't quote the code that is, do this:

var checked = $("input:checked");
checked.each(function(index) {
  var $this = $(this);
  var id_bookslot = data + index + 1;          // <== Using the index here
  var treatment_type = $this.closest("div").attr("id");
  var id_treatment = $this.attr("class");
  $.post("include/get_booking.php?insert", {
      id_bookslot:    index,
      id_treatment:   id_treatment,
      treatment_type: treatment_type
    }
  );
});

...and use checked.length for your counter variable.

过期情话 2024-11-09 15:09:19

您必须将变量移到闭包之外:

var id_bookslot = 0;

$('input:checked').each(function(){
    id_bookslot++;
    // The rest of your code
});

虽然这可能有效,但对我来说总是有点 hack-y。您可能需要考虑另一种更简洁的方法来实现您的目标(例如使用传统的 for 循环,以便您可以使用当前索引)。

You would have to move the variable outside the closure:

var id_bookslot = 0;

$('input:checked').each(function(){
    id_bookslot++;
    // The rest of your code
});

While this may work, it always seems a bit hack-y to me. You might want to think about another cleaner way to accomplish your goal (like using a traditional for loop so you have the current index available to you).

半夏半凉 2024-11-09 15:09:19

使用闭包:

var id_bookslot = 0;
$("input:checked").each(function() {

   id_bookslot++;

   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
   });
});

请注意,我删除了您的 count 变量,该变量始终为 1(回调中的 $(this) 是迭代中的单个元素),并且从来没有用过的。

Use the closure:

var id_bookslot = 0;
$("input:checked").each(function() {

   id_bookslot++;

   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
   });
});

Note that I removed your count variable, which was always 1 ($(this) inside the callback being the individual element in the iteration) and which was never used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文