我在.cshtml文件中有这段代码:
var peopleList = $('#PeopleListTable').dataTable({
// not relevant
"fnRender": function (oObj) {
var documentiddata = oObj.aData[0];
var notesdata = (oObj.aData[2]);
//alert(notesdata);
if (notesdata != null) {
var image = "images/AR/Check-on.png";
// return '<a href="#" id="' + notesdata + '" onclick="return ShowNotes(this);">' + '<img src="' + image + '" />' + '</a>';
return '<p><a onmouseout="return hideNotePopup();" onmouseover="return showNotePopup(notesdata, event);" href="javascript:void(0);" id="' + documentiddata + '">' + '<img src="' + image + '" />' + '</a></p>'
} else {
return '<a href="#" id="' + documentiddata + '"> ' + '<img src="images/AR/Check-off.png" />' + '</a>';
}
}
},
{ "sName": "OfficerName", sType: "string", sWidth: "12%" },
{ "sName": "CreateDate", sType: "string", sWidth: "15%" },
{ "sName": "FinalizedDate", sType: "string", sWidth: "15%" },
{ "sName": "TransferDate", sType: "string", sWidth: "15%" },
{ "sName": "AgencyOri", sType: "string", sWidth: "10%" }
]
});
然后在JavaScript中使用此代码:
function showNotePopup(notesdata, e) {
$("#NoteDialog").dialog('close');
$("#NoteDialog").removeClass("ui-icon ui-icon-closethick");
$("#NoteDialog").dialog({
autoOpen: false,
modal: true,
resizable: false,
position: [e.pageX, e.pageY-190]
});
$("#NoteDialog").dialog('open');
document.getElementById("note").innerHTML = notesdata;
}
此代码的目标是将鼠标悬停在数据表中的注释图像上,然后弹出窗口显示注释的内容。如果我有alert(notesdata)
,说明会正确显示。但是,当我将鼠标悬停在图像上并检查控制台时,它表示notesdata
调用中未定义showNotePopup()
。我也试过传递this
和oObj
,但无济于事。如何从cshtml中获取notesdata
到javascript函数?
答案 0 :(得分:3)
变量notesdata
仅存在于函数fnRender
的上下文中。事件onmouseover
在不同的上下文中执行,因此变量超出范围。你需要改变
onmouseover="return showNotePopup(notesdata, event);"
通过
onmouseover="return showNotePopup("' + notesdata + '", event);"