doumeitang572461 2018-09-08 01:42
浏览 34
已采纳

如何在计数超出限制后添加课程

i want to add class text-danger when i type more than max word in textarea.

here what i try

var max_chars = 230;
document.getElementById('description').onkeyup = function () {
  document.getElementById('count_char').innerHTML = (max_chars - this.value.length) + "/";
};
$('#description').on('textarea', function () {
  checkWord();
});

function checkWord(){
  var maxword = 230;
  var note = document.getElementById("descnote");
  if (minword < Number($('#description').val().length)){
    note.classList.add("text-danger");
  }
}

my form

<textarea class="form-control" name="description" id="description" form="tambah_post" rows="3" required></textarea>
<small id="count_char"></small><small id="descnote">230 Sisa Rekomendasi.</small>
  • 写回答

3条回答 默认 最新

  • douchushao7799 2018-09-08 01:48
    关注

    You may do it using only jquery. Add the keyup event listener to the text area and check the length of it's value

    var maxword = 10;
    $('#description').on('keyup', function() {
      //remove white space and check if length is greater
      if ($(this).val().trim().length > maxword) {
        //using addclass to add a class
        $(this).addClass('text-danger')
      } else {
        $(this).removeClass('text-danger')
    
      }
    });
    .text-danger {
      border: 1px solid red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <textarea class="form-control" name="description" id="description" form="tambah_post" rows="3" required></textarea>
    <small id="count_char"></small><small id="descnote">230 Sisa Rekomendasi.</small>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机