当我的字符数达到特定数字时,使用jQuery更改文本

问题描述 投票:-1回答:1

我为我的textarea做了一个单词计数器。我一直在尝试更改jQuery,以便在获得特定数量的字符时显示不同的警报但我未能实现此目的。例如,我希望将写作变为红色并在60个字符处说“这将变成两条SMS消息”。我该怎么做?

我该怎么做呢?

$(function() {
    var sms_max = 99;
    $('#sms_feedback').html(sms_max + ' characters remaining');

    $('#sms').keyup(function () {
        var text_length = $('#sms').val().length;
        var text_remaining = sms_max - text_length;

        $('#sms_feedback').html(text_remaining + ' characters remaining');
    });
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>



<button type="button" class="btn btn-link" style="margin-left: -9px; margin-bottom: 3px;" data-toggle="modal" data-target="#exampleModal">add one here</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-7">
        <textarea id="sms" style="height: 200px; width: 352px;" placeholder="Hello {clientName}."></textarea>
        <div id="sms_feedback"></div>
      </div>
	  </div></div> 
      </div>
    </div>
  </div>
</div>
javascript jquery css twitter-bootstrap
1个回答
1
投票

您只需要在text_length上添加一个比较并再执行一行来附加额外的文本,并在输入60个字符时更改字体颜色。请参阅代码中的注释。

$(function() {
  var sms_max = 99;
  $('#sms_feedback').html(sms_max + ' characters remaining');

  $('#sms').keyup(function () {
    var text_length = $('#sms').val().length;
    var text_remaining = sms_max - text_length;

    // Set the color to black at first.
    $('#sms_feedback').html(text_remaining + ' characters remaining.').css({"color":"black"});;
    
    // At 60 chars or more, add text and change the color to red.
    if(text_length >= 60){
      $('#sms_feedback').append(" This will turn into two SMS messages.").css({"color":"red"});
    }
  });
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>



<button type="button" class="btn btn-link" style="margin-left: -9px; margin-bottom: 3px;" data-toggle="modal" data-target="#exampleModal">add one here</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-7">
        <textarea id="sms" style="height: 200px; width: 352px;" placeholder="Hello {clientName}."></textarea>
        <div id="sms_feedback"></div>
      </div>
	  </div></div> 
      </div>
    </div>
  </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.