【问题标题】:how to inject javascript into a HTMLhelper text field如何将 javascript 注入 HTMLhelper 文本字段
【发布时间】:2019-04-23 08:19:32
【问题描述】:

我使用 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>

我想在文本字段中添加上述函数

【问题讨论】:

  • 只需将您的 js 函数调用用引号括起来,如下所示:"UpdateLength(600, 'ShortSummary', 'count')"
  • 我试过没用

标签: javascript asp.net-mvc html-helper


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    相关资源
    最近更新 更多