首页 > 解决方案 > 根据在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>

标签: javascripthtmlinputthymeleaf

解决方案


$(selector).attr(maxlength,value)

在价值方面,您可以提供实际价值。例如:

$("#inputID").attr(maxlength,50)

推荐阅读