2016-02-11 29 views
0

我有一個JavaScript程序,直到我試圖改變這個:"foldername"到這個:http://hokuco.com/test/"+"foldername"+"/index.html"。 我的代碼有什麼問題? 任何有興趣整個JS:javascript將字符串添加到變量錯誤

 document.getElementById("submit").addEventListener("click", function(){ 
 
     var url = document.getElementById("http://hokuco.com/test/"+"foldername"+"/index.html").value; 
 

 
     window.location.href = "url"; 
 
    });
<input type id="foldername"></input> 
 
<input type ="button" id ="submit/>

+0

那麼這將是一個非常不尋常的ID。你知道'.getElementById()'是做什麼的嗎?你也有'url'引號,所以它不會使用變量,如果這是你的意圖。 –

回答

1

你大概的意思是:

document.getElementById("submit").addEventListener("click", function(){ 
    var url = "http://hokuco.com/test/" + document.getElementById("foldername").value + "/index.html"; 

    window.location.href = url; 
}); 

變化:

  • getElementById函數的參數是一樣的id爲 「文件夾名」 輸入元素的ID屬性。
  • window.location.href應該設置爲一個變量,而不是引用的字符串。

更清晰,你會想:

document.getElementById("submit").addEventListener("click", function(){ 
    var folder = document.getElementById("foldername").value; 
    var url = "http://hokuco.com/test/" + folder + "/index.html"; 

    window.location.href = url; 
}); 

現在,希望它更清楚發生了什麼事情。

-2

什麼是你想實現什麼?它看起來像你試圖重定向,但使用字符串文字而不是變量。

相關問題