jQuery $ .each循环没有执行

时间:2016-05-13 18:17:24

标签: javascript jquery

在下面的代码中,每个循环都没有被执行。执行循环上方和下方的代码。无法弄清楚原因。

GM_xmlhttpRequest({
  method: "GET",
  url: link,
  synchronous: true,
  onload: function(response) {

    //console.log($(response.responseText).find( "#P16_STATUS2" ).text());
    var status = "NOT FOUND";
    if ($(response.responseText).find("#P16_STATUS2").size() === 1) {
      status = $(response.responseText).find("#P16_STATUS2").text();
      console.log("Status is " + status);
    } else if ($(response.responseText).find("#P16_STATUS3").size() === 1) {
      status = $(response.responseText).find("#P16_STATUS3").val();
      console.log("Status is " + status);
    }
    alert("one");
    //for (var i = 0; i < requestArray.length; i++) {      
    $.each(requestArray, function(key, value) {
      alert("in each");
      console.log("inside for");
      if (key === val) {
        console.log("request id found already present in array");
        return false; // <=== breaks out of the loop early
      } else {
        console.log("request id not found, Hence adding" + newArray[i]);
        var pusharr = [val + "-" + status];
        requestArray.push(pusharr);
        console.log("Updated Array is " + requestArray);
      }
    });


    alert("out");
    linkTag.attr("title", status);

  }
});

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

确保foreach($aGente as $k => $v){ if($k => $v){ echo $v; } } 不为空。 jQuery不会在空对象/数组上执行每个函数。