2017-12-14 15 views
0

フォーラムにいくつかの絵文字を実装しようとしていますが、私のJSコードは機能していないようです。 JS:TextAreaForでの絵文字の入力

$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> 

ここに取り引きは何ですか?私は間違ったトラックにいますか?おかげさまで

答えて

0

あなたはまだ正しいトラックを使用していますが、name属性はPOSTリクエストでユーザーの入力名を決定するため、id属性を使用する方がより好きです。

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

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

は、その後、あなたがテキストエリアに絵文字を追加するには、このスクリプトを使用することができます:

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

ライブの実装:.NET Fiddle example

まず、両方のsmileyボタン&テキストエリアにid属性を置きます。

参考:

How to append text to '<textarea>'?

関連する問題