javascript - 根据在javascript中选择的输入语言设置输入文本的长度
问题描述
我有一个 div,其中有一个输入给用户。我必须应用一个验证,如果输入是英文,则限制用户输入 50 个单词,否则如果输入是“日文”,那么它应该只允许 35 个单词。所以我必须为这两个输入设置验证。目前存在限制,它根据网站设置进行限制,即如果网站设置为英语,那么无论“英语”或“日语”输入什么内容,它都只允许 50 个单词。所以我想限制输入而不是应该基于网站设置
在这里,我想在点击事件或类似事件上添加一些 JS 代码,以使 th:maxlength 动态化,以便可以根据“alertmessage”字段中的输入来限制长度
$('#alertMessage').on("click", function(e) {
$("#sendMessageDuringThunder").addClass("text-white");
$('#alertMessage').addClass('highlight-input-message');
$('#alertMessageTable').fadeIn().css('display', 'inline-block');
$('.notification-div').removeClass('rounded-bottom');
$('.noti-alert-dismissible').removeClass('rounded-bottom rounded-right');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="input-inline-class" th:if="${notification.notification=='thunder-alert'}">
<th:block th:with="maxLength=${session.usercontext.getDefaultLanguage()=='en'?50:35}">
<div th:classappend="${notification.currentStatus}?visible:hide" id="alertMessageBlock">
<input id="alert-issued-on" type="hidden" th:attr="issuedOn=${notification.generatedOn}" />
<input id="alertMessage" autocomplete="off" class="d-down-none input-message" th:maxlength="${maxLength}" name="alertMessage" type="text" th:placeholder="#{label.write.message}" />
<i id="sendMsgIP" class="fas fa-spinner fa-lg fa-spin cursor hidden d-down-none px-1"></i>
<i class="fas fa-chevron-circle-right cursor notification-alert-heading-grey d-down-none px-1 fa-lg" id="sendMessageDuringThunder"></i>
</div>
</th:block>
</div>
解决方案
$(selector).attr(maxlength,value)
在价值方面,您可以提供实际价值。例如:
$("#inputID").attr(maxlength,50)
推荐阅读
- python - Python:重建嵌套列表
- informix - 在我使用卸载 informix 语句时定义具体的编码
- node.js - 出现致命错误:区域分配失败 - 尝试在 nodejs 中安装永远运行的模块时进程内存不足
- asp.net-core - ASP.NET Core 2.1 无法从托管服务访问数据库上下文
- dialogflow-es - DialogFlow Intent API - `intent.userSays` 中的对象字段代表什么?
- python - Pycharm:无法使用命令行参数运行单元测试
- mysql - mySQL不同的ORDER BY基于不同的WHERE
- c# - C#:在多个进程和/或线程之间共享数据的最佳方式
- protractor - 量角器为每次测试启动新浏览器
- ruby-on-rails - 实例化大量对象时避免rails中内存膨胀的方法?