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

时间:2018-03-25 17:56:36

标签: javascript jquery css twitter-bootstrap

我为我的textarea做了一个单词计数器。我一直在尝试更改jQuery,以便在获得特定数量的字符时显示不同的警报但我未能实现此目的。例如,我希望将写作变为红色并说“#34;这将变成两条短信”#34;在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;

        $('#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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

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

&#13;
&#13;
$(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"});
    }
  });
})
&#13;
<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>
&#13;
&#13;
&#13;