首页 > 解决方案 > 如何在引导程序 maxlength 上手动设置当前键入的字符?

问题描述

https://jsfiddle.net/0tx58ma1/6/

(function($){
  $('[maxlength]').maxlength({
    alwaysShow: true,
    separator: ' / ',
    preText: '',
    postText: '',
    showMaxLength: true,
    showCharsTyped: true,
    placement: 'bottom-right-inside',
    showOnReady: true,
  });

  $("#btnReplaceText").click(function(){
    $("#test").val("ypaaaa");
    $("#text").show();
  });
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-alpha1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-maxlength/1.10.0/bootstrap-maxlength.min.js"></script>
<input type="text" id="test" maxlength="10" placeholder="please click button" value="">
<br>
<button type="button" id="btnReplaceText">click me</button>
<p id="text" style="display: none;">I wanna set current typed characters when I push button. How can I ???</p>

该插件实时计算当前输入的字符数。

如果您使用键盘打字,它会按您的预期计算字符数。

但是,如果您使用 jQuery 将字符串替换为单击事件,则它不会计算字符数,因为它没有输入。

我希望单击按钮时当前字符数出现在字符计数器中。

当我按下按钮时,我想设置当前输入的字符。我怎样才能 ???

标签: javascripthtmljquerytwitter-bootstrap

解决方案


我从开发人员那里听到了这个问题的解决方案。

似乎我们可以通过执行以下操作来计算字符数。

$("#test").val("ypaaaa") 

to

$("#test").val("ypaaaa").trigger('input') 

推荐阅读