变量在' for循环中重置'

时间:2017-08-17 14:23:36

标签: javascript database for-loop calendar phonegap

我将信息从数据库推送到日历,其中每个日期的id等于数据库中的id。代码遍历数据库搜索匹配对。如果找到匹配项,则将图像的名称放入日历中的图像标记中。但是,默认图像是唯一显示的图像,并且变量calImg似乎正在重置为默认值。 var idCalendar = []; var arrayCalendar = -1; var calImg;是全局变量



while (counter <= numOfDays) {
  var dateID = counter + monthIDName;
  idCalendar[counter - 1] = dateID;
  //alert(idCalendar[counter-1]);
  // When to start new line.
  if (weekdays2 > 6) {
    weekdays2 = 0;
    htmlContent += "</tr><tr>";
  }
  //check to ensure the mydb object has been created
  /////////////////////////////////////////////////////////////////////////////
  if (mydb) {
    //Get all the entry from the database with a select statement
    mydb.transaction(function(t) {
      t.executeSql("SELECT rating FROM entry WHERE calendar=?", [idCalendar[arrayCalendar + 1]], function(t, results) {
        var i;
        //Iterate through the results
        if (results.rows.length != 0) {
          for (i = 0; i < results.rows.length; i++) {
            var row = results.rows.item(i);
            calImg = row.rating;
            alert(idCalendar[arrayCalendar] + calImg);
          }
        } else {
          alert(idCalendar[arrayCalendar]);
          calImg = 'default';
        }
      });
      arrayCalendar++;
    });
  } else {
    myApp.alert("query error");
  }
  ////////////////////////////////////////////////////////////////////////////////////////////////////
  // if counter is current day.
  // highlight current day using the CSS defined in header.
  if (counter == day && month == curMonth && year == curYear) {
    htmlContent += "<td class='dayNow' id='" + counter + monthIDName + "'  onMouseOver='this.style.background=\"#FF0000\"; this.style.color=\"#FFFFFF\"' " + "onMouseOut='this.style.background=\"#FFFFFF\"; this.style.color=\"#FF0000\"'>" + counter + "<img src= 'img/" + calImg + ".png' align='left' height= '15' width= '15'>" + "<br>" + "</td>";
  } else {
    htmlContent += "<td class='monthNow' id='" + counter + monthIDName + "' onMouseOver='this.style.background=\"#FF0000\"'" + " onMouseOut='this.style.background=\"#FFFFFF\"'>" + counter + "<img src= 'img/" + calImg + ".png' align='left' height= '15' width= '15'>" + "<br>" + "</td>";
  }
  weekdays2++;
  counter++;
}
&#13;
&#13;
&#13;

0 个答案:

没有答案