簡體   English   中英

使用 Jquery 限制輸入文本

[英]Limit input text using Jquery

我正在使用 Tawk 在我的網站上聊天。 我想將文本數量限制為 11 位數字。

<input type="text" id="prechat2Field" title="Phone" value="" class="textInput" maxlength="20" placeholder=" Phone">

我無法更改他們的核心文件。 可以使用 Jquery 完成嗎?

您可以覆蓋該元素的屬性maxlength

 $('#prechat2Field').prop('maxlength',11);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <input type="text" id="prechat2Field" title="Phone" value="" class="textInput" maxlength="20" placeholder=" Phone">

您可以通過 jQuery 像下面這樣設置maxlength屬性:

 $(document).ready(function(){ $("#prechat2Field").attr('maxlength',"11"); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <input type="text" id="prechat2Field" title="Phone" value="" class="textInput" maxlength="20" placeholder=" Phone">

<input type="text" id="prechat2Field" title="Phone" value="" class="textInput" maxlength="20" placeholder=" Phone">

要通過 jQuery 做到這一點:

function restrict_length(value, maxChar){
    $(value).attr('maxlength',maxChar);
}

另一種方法(沒有maxlength ):

 var input = document.getElementById('prechat2Field') input.onkeypress = function(ev) { if (input.value.length >= 11) { return false } }
 <input type="text" id="prechat2Field" value="" placeholder="Phone">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM