[英]Input formatting jQuery
我正在使用此插件: jQuery-Mask-Plugin格式化輸入。
我可以正常使用,但問題是我希望范圍從0.0到6.0最大值開始。 這意味着當我放6時,我只能寫6.0,而不能寫6.1或6.3,...與0相同。
演示
$('#test_numeral').mask('a.9', { 'translation': { a: { pattern: /[0-6]/ } } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.js"></script> <input type="text" id="test_numeral">
有沒有辦法使它與此插件一起使用,或者您知道可以執行此操作的插件嗎?
如果大於6.0,則可以使用回調函數並更改其值
$('#test_numeral').mask('a.9', { 'translation': { a: { pattern: /[0-6]/ } }, onKeyPress: function(cep, event, currentField, options) { if (cep > 6) { currentField.val('6.0'); } } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.js"></script> <input type="text" id="test_numeral">
正如Pavan Sikarwar指出的那樣,使用回調函數非常好。
與上面的腳本幾乎相同,但是我沒有將值設置為6.0,而是使您不能鍵入最后一個小數。
let opts = {
'translation':
{
a:
{
pattern: /[0-6]/
}
},
onKeyPress: function(cep, event, currentField, options){
if(cep > 6.0) { currentField.val('6.'); }
}
}
$('#test_numeral').mask('a.0', opts);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.