【问题标题】:How can I set current typed characters manually on bootstrap maxlength?如何在引导程序 maxlength 上手动设置当前键入的字符?
【发布时间】:2020-07-14 15:40:07
【问题描述】:

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 将字符串替换为单击事件,则它不会计算字符数,因为它没有输入。

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

我想在按下按钮时设置当前输入的字符。我怎么能???

【问题讨论】:

  • 能否请您更清楚地说明您的问题陈述?

标签: javascript html jquery twitter-bootstrap


【解决方案1】:

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

看来我们可以通过以下操作来统计字符数。

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

to

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

【讨论】:

    猜你喜欢
    • 2011-01-28
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多