2016-12-03 67 views
0

我正在創建一個調查應用程序,並且我正在使用每個調查的模板標記來促進重複使用。我有一個用於所有調查的html文件,我在運行時填充它們,具體取決於按下哪個按鈕以使用它們的特定ID選擇調查。獲取被按下的按鈕的屬性ID,該按鈕被重定向到不同的頁面(需要在重定向頁面加載時)

實施例:

<a href="SurveyTemplate.html" id = 'survey1'</a> 
<a href="SurveyTemplate.html" id = 'survey2'</a> 

在surveyTemplate,我已插入了一塊一個腳本,包括

window.onload = function() { 
// event.target.id should be survey1 or survey2, is showing up undefined 
    generateQuestionsAndAnswers(idToSurveyMap[event.target.id]); 

}; 

其中generateQuestionAndAnswers是用於填充從調查數據的陣列使用調查的函數。

idSurveyMap是一個將html元素(字符串)的id映射到調查數據(這是一個數組)的映射。

我遇到麻煩的重要想法是獲取按下的鏈接的ID。 Event.target.id給我未定義。我無法通過這種方式訪問​​它嗎?有另一種方法嗎?

+0

如果一個新的頁面加載,當你點擊鏈接,你將無法在未來的鏈接訪問該事件。但是,您可以設置鏈接的格式,以便調查編號爲網址本身,例如「SurveyTemplate.html?num = 1」或「SurveyTemplate.html#1」 – fredrover

回答

0

如何使用get參數?

<a href="SurveyTemplate.html?id=1">survey1</a> 
<a href="SurveyTemplate.html?id=2">survey2</a> 

window.onload = function() { 
    var surveyId = window.location.search.replace('?id=', ''); 
    generateQuestionsAndAnswers(idToSurveyMap[surveyId]); 
}; 
相關問題