在for循环中输入onchange更改循环中的最后一个变量?

时间:2016-08-23 01:31:14

标签: javascript function dom onchange

大家好,我对javascript相对较新,我正在尝试使用带有onchange事件的输入范围,但是我将它们放在一个循环中,这些信息来自我从数据库中得到的信息类似

var splitstring = response.split(String.fromCharCode(30));
for (var i = 0; i < splitstring.length; i++) {
  type = details[0];
  actualId = details[1];
  userId = details[2];
  time = details[3];
  //that's user details or whatever so i make the H2 and the range   


  var range = document.createElement("INPUT");
  var spanchange = document.createElement("H2");
  spanchange.appendChild(document.createTextNode("0 - neutral"));
  spanchange.style.cssFloat = "right";
  range.setAttribute("type", "range");
  range.setAttribute("class", "horizontal-lowest-first");
  range.setAttribute("min", "-100");
  range.setAttribute("value", "0");
  range.setAttribute("name", "range");
  var change = function() { ///range onchange to php :)
    var ranges = spanchange;
    var x = this.value;
    var then = x;

    if (x < -90) {
      then = x + " : " + "Extremely Poor";
    } else if (x < -80) {
      then = x + " : " + "Very Poor";
    } else if (x < -70) {
      then = x + " : " + "Pretty Bad";
    } else if (x < -60) {
      then = x + " : " + "Bad";
    } else if (x < -50) {
      then = x + " : " + "Not Good";
    } else if (x < -40) {
      then = x + " : " + "Phhhhst";
    } else if (x < -30) {
      then = x + " : " + "Oh No!";
    } else if (x < -20) {
      then = x + " : " + "Needs Work";
    } else if (x < -20) {
      then = x + " : " + "Crap";
    } else if (x < -10) {
      then = x + " : " + "Welp";
    } else if (x < 0) {
      then = x + " : " + "Downvote";
    } else if (x == 0) {
      then = x + " : " + "Neutral";
    } else if (x < 10) {
      then = x + " : " + "OH";
    } else if (x < 20) {
      then = x + " : " + "Better";
    } else if (x < 30) {
      then = x + " : " + "Yay";
    } else if (x < 40) {
      then = x + " : " + "Pretty Good";
    } else if (x < 50) {
      then = x + " : " + "Mediocre";
    } else if (x < 60) {
      then = x + " : " + "Great";
    } else if (x < 70) {
      then = x + " : " + "Yes!";
    } else if (x < 80) {
      then = x + " : " + "Awesome";
    } else if (x < 90) {
      then = x + " : " + "Very Good";
    } else if (x < 100) {
      then = x + " : " + "Top Grade";
    } else if (x == 100) {
      then = x + " : " + "Perfect!!";
    }
    console.log("inside onchange" + "    " + then);
    // var x = spanchange.childNodes[0];
    // x.replaceData(0, 20,then);
    ranges.innerHTML = then;
  };
  range.onchange = change;
}

然后它将元素添加到div并将div附加到正文然后再次循环 所以我在onchange函数中放了

ranges.innerHTML = then;

它总是在最后的for循环迭代中改变spanchange,所以我的意思是如果我改变第一个附加的范围,它设置最后一个附加的spanchange的值,但我希望它改变在同一迭代中的spanchange对于那个范围的循环,它听起来很疯狂,我知道我正在尝试解释,我希望有人能理解

如果你能给我任何建议,我非常感谢你,谢谢你

0 个答案:

没有答案