[英]If input type is set to Number how to set maxlength using sapui5
[英]How to set a maxLength of NUMBER input with jQuery
聽起來微不足道,但對我而言不起作用...我試圖設置NUMBER輸入的最大長度,但不起作用。 感謝您的幫助。 這是我的代碼。 兩者都無法在Chrome中使用
<input id="demo02" type="number">
$(document).ready(function(){
$("#demo02").attr('maxlength','2');
});
$(document).ready(function(){
$("#demo02").prop('maxlength','2');
});
對於input type="number"
maxlength
不是有效的屬性。
考慮到這一點,我們將使用有效的max
屬性獲取其值的長度,並將其用作input
事件偵聽器中的maxlength。
我們將定位具有max
屬性集的類型為number
的input
元素:
$('input[type=number][max]:not([max=""])')
代碼段:
$(document).ready(function() { $('input[type=number][max]:not([max=""])').on('input', function(ev) { var $this = $(this); var maxlength = $this.attr('max').length; var value = $this.val(); if (value && value.length >= maxlength) { $this.val(value.substr(0, maxlength)); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" max="99999">
使用max
屬性限制最大可用數量。 如果將max
設置為99
,則不能再提高了。
$("#demo02").attr('max','99');
或使用純Javascript:
document.getElementById('demo02').setAttribute('max', 99);
document.getElementById('demo02').setAttribute('max', 99);
<input id="demo02" type="number">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.