我在 .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 函数?
最佳答案
变量notesdata
仅存在于函数fnRender
的上下文中。事件 onmouseover
在不同的上下文中执行,因此该变量超出了范围。你需要改变
onmouseover="return showNotePopup(notesdata, event);"
由
onmouseover="return showNotePopup("' + notesdata + '", event);"
关于javascript - 通过 .cshtml 文件中的 HTML 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22872784/