简体   繁体   English

输入格式jQuery

[英]Input formatting jQuery

I am using this plugin: jQuery-Mask-Plugin to format inputs. 我正在使用此插件: jQuery-Mask-Plugin格式化输入。

I got it working but the problem is that i want to have a range that begins from 0.0 to 6.0 maximum. 我可以正常使用,但问题是我希望范围从0.0到6.0最大值开始。 It means when i put 6 i can only write 6.0 and no 6.1 or 6.3,... It is the same with 0. 这意味着当我放6时,我只能写6.0,而不能写6.1或6.3,...与0相同。

demo 演示

 $('#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"> 

Is there a way to make it work with this plugin or do you know such plugins that can do this thing? 有没有办法使它与此插件一起使用,或者您知道可以执行此操作的插件吗?

You can use callback function and change its value if greater than 6.0 如果大于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"> 

As Pavan Sikarwar pointed out, it's great to use a callback function. 正如Pavan Sikarwar指出的那样,使用回调函数非常好。

CodePen Link CodePen链接

Pretty much same script as above but instead of setting the value to 6.0, I made is so that you can't type the last decimal. 与上面的脚本几乎相同,但是我没有将值设置为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM