我正尝试使用onclick事件将字符串传递到函数中。但是,当我点击链接时,该函数内的变量不会返回任何内容。它是空的。当我将一个数字作为参数传递并且没有问题显示在警报中时,我没有任何问题。我不确定我做错了什么。当通过onclick函数传递时,包含字符串的变量为空
for (var j=0;j<current_order.length;j++){
var note_id = "note" + (j+1);
text = text + "<button onClick= 'deleteItemfromOrder("+j+")'>Delete</button>";
text = text + "<a id = '"+note_id+"' onClick= 'toggleNote("+note_id+")'>Notes</a>";
alert(text);
}
这是我试图note_id传递给函数:
function toggleNote(showHideDiv) {
alert(showHideDiv);
}
我曾尝试多种方法将字符串作为参数传递给函数EG:
text = text + "<a id = '"+note_id+"' onClick= 'toggleNote('"+note_id+"')'>Notes</a>";
和
text = text + "<a id = '"+note_id+"' onClick= 'toggleNote(\'"+note_id+"\;)'>Notes</a>";
H无论何时该字符串在函数的另一侧显示为空。有没有问题,如果我通过一些如:
text = text + "<a id = '"+note_id+"' onClick= 'toggleNote("+2+")'>Notes</a>";
也可以有人说为什么这已经downvoted? –