通过.cshtml文件中的HTML传递参数

时间:2014-04-04 20:51:11

标签: javascript jquery

我在.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()。我也试过传递thisoObj,但无济于事。如何从cshtml中获取notesdata到javascript函数?

1 个答案:

答案 0 :(得分:3)

变量notesdata仅存在于函数fnRender的上下文中。事件onmouseover在不同的上下文中执行,因此变量超出范围。你需要改变

onmouseover="return showNotePopup(notesdata, event);"

通过

onmouseover="return showNotePopup(&quot;' + notesdata + '&quot;, event);"