2016-07-15 20 views
2

我正在使用JavaScript填充<div>標記與其他<div> s。它一直在工作,直到我更改了onclick事件中使用的標識符。舊名稱(指數)從0-1000只是一個小數目,但新標識(ID)是uuid.v4()生成的字符串,它看起來像這樣:失蹤)參數後,但只爲一個長字符串

f5ec8170-e75c-4a93-9997-1a683b7d2e00 

我有相同的代碼索引和id。但每當我點擊它是假設激活函數調用的ID作爲自變量,它給我的按鈕:爭吵後

失蹤)

這不,當我點擊發生按鈕與索引做同樣的事情,而不是id。

我的代碼:

var id = messages[i].id; 
var index = 0; 

var newElement = 
           '<div class="fullMessage" id="fullRightMessage' + i + '">'+ 
            '<h6 class="textMessage">' + messages[i].comment + '</h6>' + 
            '<button class="likeButtonMessage" onclick="likeClicked(right, ' + index + ', 1);">LIKE</button>' + 
            '<button class="dislikeButtonMessage" onclick="likeClicked(right, ' + id + ', -1);">DIS</button>' + 
            '<h4 id="scoreright' + i + '" class="messageScore">' + messages[i].score + '</h4>' + 
           '</div>' 
+3

您需要通過'串''''''''''''''按鈕''''按鈕onclick =「likeClicked(right,\''+ index +'\',1);」> LIKE'', – Satpal

+1

@Satpal typo?編號,而不是索引,這將產生不良的HTML - 你需要轉義撇號,而不是加雙引號 –

回答

3

你沒有用引號括起來的uuid。在它工作之前,因爲你的ID是一個乾淨的整數,不需要它們。

交流與ID行至

'<button class="dislikeButtonMessage" onclick="likeClicked(right, \'' + id + '\', -1);">DIS</button>' + 
+0

非常感謝你! –

+0

很高興我能幫上忙。 – TehSphinX

0

你以前的ID被解釋爲int,這就是爲什麼它的工作的原因。

您的新ID是string,需要你把它們放在引號:

onclick="likeClicked(right, \'' + id + '\', -1);" 

這是因爲這是不是有效的代碼:

likeClicked(right, f5ec8170-e75c-4a93-9997-1a683b7d2e00, -1) 
相關問題