[英]Running javascript function conditionally - Razor, HTML
现在,我有一个使用用户选择和数据动态创建的表。 有时,数据有错误,我想在表中告知用户。
剃刀(链接将您带到〜/ ControllerName / givenID)
<tr id="@givenID">
<td>@Html.ActionLink(name, someMethod, "ControllerName", new { id = givenID }, new { title = "", id = givenID })</td>
@if (errorConditions)
{
<div id="@givenID" onload="addErrorMessage(this)" data-test="ErrorMessage" style="display: none"></div>
}
</tr>
Java脚本
$(function () {
//adding custom tooltip to document
$(document).tooltip();
//adding error message to standard
function addErrorMessage(element) {
var theElemOfID = document.getElementById(element.id);
theElemOfID.title = element.attr("data-test");
theElemOfID.style.color = rgb(255, 0, 0);
} });
我正在尝试为ActionLink添加标题(以便它可以由工具提示使用),并且还更改链接的颜色,但是仅当errorConditions为true时才可以。
现在,当我跑步时,什么也没发生。 addErrorMessage从不调用,但是所有信息都位于我期望的位置(如链接中具有正确的ID等)。
有人有主意吗?
onload
仅在<body>
上受支持,但是强烈建议您不要使用内联事件处理程序!
使用内联脚本,如下所示(使用jQuery,因为您似乎正在使用它):
@if (errorConditions)
{
<div id="@givenID" data-test="ErrorMessage" style="display: none"></div>
<script>
$(function() {
addErrorMessage($('#@givenID'));
});
</script>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.