在TextAreaFor中输入表情符号

时间:2017-12-14 17:40:47

标签: javascript html asp.net razor

我试图在我的论坛中实现一些表情符号,但我的JS代码似乎无法正常工作。 JS:

$button = $('button[name="smiley"]')
$('button').on('click', function () {
$('textareafor[name="content"').append(":)")})

HTML:

@using Reddit.Models
@model CreateTopicViewModel

<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <section id="postForm">

            @using (Html.BeginForm("CreateTopic", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                <div class="form-group">
                    @Html.LabelFor(x => x.Name, new { @class = "control-label" })
                    @Html.TextBoxFor(x => x.Name, new { @class = "form-control" })
                </div>             
                <button name="smiley">s</button>
                <div class="form-group" name="content">
                    @Html.LabelFor(x => x.Content, new { @class = "control-label" })
                    @Html.TextAreaFor(x => x.Content, new { @class = "form-control", @rows = "8" , name = "content" })
                </div>
                <button type="submit" class="btn btn-block btn-primary">Post Topic</button>
            }
        </section>
    </div>
</div>

这里的交易是什么?我走错了路吗? 感谢。

1 个答案:

答案 0 :(得分:0)

您仍然处于正确的轨道,但由于id属性确定了POST请求中用户的输入名称,因此更倾向于使用name属性。

首先,将id属性放在smiley按钮&amp; textarea的:

<button id="smiley">s</button>

@Html.TextAreaFor(x => x.Content, new { @class = "form-control", @rows = "8", id = "content" })

然后您可以使用此脚本将表情符号添加到textarea:

<script>
    $('#smiley').click(function () {
        var textarea = $('#content');
        textarea.val(textarea.val() + ":)");
    });
</script>

实时实施:.NET Fiddle example

参考:

How to append text to '<textarea>'?