2016-12-06 17 views
1

我有這個設置的功能,它拒絕運行JavaScript的令人興奮的行爲

alert('Ok I reach in if 1') 

alert('Ok I reach in if 2') 

儘管我沒有HTML或JavaScript錯誤。

警報(RES [3])之前,如果輸出 '巴扎'

警報(typeof運算資源[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

+0

有時候出於某種原因TEH錯誤不顯示在控制檯中,你必須把包含在try..catch和提醒消息。 –

+0

使用'console.log()'而不是'alert()',這樣你就可以看到字符串中的所有字符。 – Barmar

回答

0

你最有可能在你的字符串的末尾有一些空格。您可以使用String.prototype.trim刪除多餘的空格。

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

+0

你是一個真正的!這是我有5空行的問題...不要現在從哪裏(PHP輸出可以)。我用.substring(0,4)刪除它們,而字符串將始終有4個字符。 – Vasia