javascript令人兴奋的行为

时间:2016-12-06 21:17:08

标签: javascript if-statement

我有这套功能但它拒绝运行

alert('Ok I reach in if 1') 

alert('Ok I reach in if 2') 

尽管我没有html或javascript错误。

如果输出'baza'

之前

警告(res [3]) 如果输出'string'

alert(typeof res [3])

function ataseazaPoza(id, poza) {
  tip = '';
  if (id.search("dragzonesortpic") == 0) {
    tip = 'sort';
  } else {
    tip = 'baza';
  }
  http.open('get', 'produse_ataseazaPoza.php?id=' + id + '&image=' + poza + '&tip=' + tip + '&nocache=' + Math.random());
  http.onreadystatechange = ataseazaPozaReply;
  http.send(null);
}

function ataseazaPozaReply() {
  if (http.readyState == 4) {
    var response = http.responseText;
    res = response.split(";");
    if (res[0] == 'er') {
      alert('Eroare: ' + res[1] + ' ' + res[2]);
    } else {
      keys = res[2].split("-");
      alert(res[3]);
      if (res[3] == 'baza') {
        alert('Ok I reach in if 1');
        elemRo = document.getElementById('rowuv' + res[1]).style.backgroundColor = 'transparent';
        elemOv = document.getElementById('dragzoneuv' + res[1]);
        imgurl = "url('../prod_imagini/" + keys[0] + "/" + keys[0] + "/" + res[2] + "')";
        elemOv.style.backgroundImage = imgurl;
      }
      if (res[3] == 'sort') {
        alert('Ok I reach in if 2');
        randPlus = parseInt(res[1]) + 1;
        elemRo = document.getElementById('sortHoverDrop' + res[1]).style.backgroundColor = 'transparent';
        elemOv = document.getElementById('dragzonesortpic' + res[1]);
        imgurl = "url('../prod_imagini/" + keys[0] + "/" + keys[0] + "/" + res[2] + "')";
        document.getElementById('pozaHiddenSort' + res[1]).value = imgurl;
        elemOv.style.backgroundImage = imgurl;
      }
    }
  }
}

为什么不输入?real code

1 个答案:

答案 0 :(得分:0)

你很可能在字符串的末尾有一些空格。您可以使用String.prototype.trim删除额外的空格。



var withWhitespace = 'baza ';
console.log(withWhitespace + ' == "baza"', withWhitespace == 'baza');
var withoutWhitespace = withWhitespace.trim();
console.log(withoutWhitespace + ' == "baza"', withoutWhitespace == 'baza');