首页 > 解决方案 > 如何将 javascript 注入 HTMLhelper 文本字段

问题描述

我使用 asp.net mvc 创建了一个表单,我正在使用实体框架数据库。在表单上有文本字段,我如何在 html 帮助器中添加 javascript。

下面是文本字段

@Html.TextAreaFor(m => m.ShortSummary, htmlAttributes: new { onkeyup = UpdateLength(600, "ShortSummary", "count" ) })
<span id="count"></span>

下面是我创建的用于计算用户键入时的字符数的函数

<script type="text/javascript">
    function UpdateLength(length, textID, spanID) {
        var text = document.getElementById(textID);
        var span = document.getElementById(spanID);

        span.innerHTML = (length - text.value.length);
    }
</script>

我想将上述功能添加到文本字段

标签: javascriptasp.net-mvchtml-helper

解决方案


这段代码对我来说很好,感谢所有帮助 <textarea asp-for="ShortSummary" class = "form-control resizer" placeholder = "Short Summary (90 words)" tabindex = 1 rows = 8 onkeyup='UpdateLength(600, "ShortSummary", "count")'></textarea>


推荐阅读