简体   繁体   English

如何...按类获取元素,获取id,并使用id作为变量传递给php

[英]How.. get element by class, grab id, and use id as variable to pass to php

I'm running a function on an interval. 我正在间隔运行一个函数。 I need to get element by class name, use the id of each and pass it into a function as a variable. 我需要逐个元素地获取元素,使用每个的id并将其作为变量传递给函数。

I've tried looping through each class name and passing the id to php but it passes an array (i think) or maybe an html index which i cant separate. 我已经尝试循环遍历每个类名并将id传递给php,但它传递了一个数组(我认为)或者可能是一个我无法分开的html索引。

$(".eachUnread").each(function() {
    var newId = this.id;
    $(".eachUnread").load("../php/unreadEach.php?id="+newId);
    console.log(newId);
});

I also tried.. 我也试过..

var x = document.getElementsByClassName("eachUnread");
var i;
for (i = 0; i < x.length; i++) {
    var newId = x[i].id;
    console.log(newId);
    $(".eachUnread").load("../php/unreadEach.php?id="+newId);
    }

When the .eachUnread div loads its cycles through all the id's. 当.eachUnread div通过所有id加载其循环时。 How do I get it to load only the correct id? 如何让它只加载正确的ID?

You need add param for each function(index, item) method and use attr for get id var newId = $(item).attr('id'); 您需要为每个function(index, item)方法添加param,并使用attr for get id var newId = $(item).attr('id');

$(".eachUnread").each(function(index, item) {
    var newId = $(item).attr('id');
    $(".eachUnread").load("../php/unreadEach.php?id="+newId);
    console.log(newId);
});

 $(".eachUnread").each(function(index, item) { var newId = $(item).attr('id'); $(".eachUnread").load("../php/unreadEach.php?id="+newId); console.log(newId); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='eachUnread' id='test1'>A</div> <div class='eachUnread' id='test2'>B</div> <div class='eachUnread' id='test3'>C</div> 

simply this... 只是这......

  $(".eachUnread").each(function() {
      $(this).load("../php/unreadEach.php?id="+this.id);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM