javascript - 如何在引导程序 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 将字符串替换为单击事件,则它不会计算字符数,因为它没有输入。
我希望单击按钮时当前字符数出现在字符计数器中。
当我按下按钮时,我想设置当前输入的字符。我怎样才能 ???
解决方案
我从开发人员那里听到了这个问题的解决方案。
似乎我们可以通过执行以下操作来计算字符数。
$("#test").val("ypaaaa")
to
$("#test").val("ypaaaa").trigger('input')
推荐阅读
- c - ffmpeg中.so文件和java库的区别
- php - 在 php 中更改 $success 消息的颜色
- c++ - Qt 4.7 QFileSystemWatcher 示例
- java - Spring Reactive WebFlux - 如何自定义 BadRequest 错误信息
- webpack - 通过 Webpack babel-loader 添加时,Promise polyfill 在 FireFox 和 IE11 中不起作用
- java - 如何使用 ! 解码 %21 swagger 编辑器中 URL 的查询参数中的符号
- sql - Teradata:将重复值转换为逗号分隔字符串的结果
- powershell - Powershell 返回错误的输出
- sql - Postgres 查询左连接耗时太长
- c++ - 无法使用 Visual Studio 2019 访问虚幻引擎 4.25 中的头文件