本文介绍了如何使用jquery应用自定义别名inputmask?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Yii2表单输入上使用inputmask.这是我的代码:

I'm trying to use a inputmask on a Yii2 form input. Here is my code:

var IDR={"alias":"numeric","prefix":"Rp","digits":0,"digitsOptional":false,"decimalProtect":true,"groupSeparator":",","radixPoint":".","radixFocus":true,"autoGroup":true,"autoUnmask":true,"removeMaskOnSubmit":true};
Inputmask.extendAliases({"IDR": {"alias":"numeric","prefix":"Rp","digits":0,"digitsOptional":false,"decimalProtect":true,"groupSeparator":",","radixPoint":".","radixFocus":true,"autoGroup":true,"autoUnmask":true,"removeMaskOnSubmit":true} }); 

以下所有内容在jquery.inputmask.bundle.js上产生错误Uncaught SyntaxError:

ALL of the following produce the error Uncaught SyntaxError on jquery.inputmask.bundle.js:

jQuery('selector').inputmask(IDR)
jQuery('selector').inputmask("IDR")
jQuery('selector').inputmask(eval(IDR))
jQuery('selector').inputmask({'mask':'IDR'})
jQuery('selector').inputmask({'alias':'IDR'})

Chrome调试器指出以下输入掩码代码行有问题:

Chrome debugger points to a problem with the following line of inputmask code:

42: dataoptions = JSON.parse("{" + attrOptions + "}")), dataoptions) {

推荐答案

我研究了jquery.inputmask 3.x的文档.据我了解,更改别名属性的首选方法是创建一个新的别名,从默认别名定义继承.

I have looked into the documentation of jquery.inputmask 3.x.From my understanding the preferred way to alter properties for an alias is by creating a new alias which inherits from the default alias definition.

示例

 Inputmask.extendAliases({
      'numeric': {
        "prefix":"Rp",
        "digits":0,
        "digitsOptional":false,
        "decimalProtect":true,
        "groupSeparator":",",
        "radixPoint":".",
        "radixFocus":true,
        "autoGroup":true,
        "autoUnmask":true,
        "removeMaskOnSubmit":true
      }
    }); 

Inputmask.extendAliases({
     'IDR': {
        alias: "numeric", //it inherits all the properties of numeric    
       "prefix":"Rpoverrided"//overrided the prefix property   
      }
});

现在将输入掩码应用到您的选择器上

Now apply the input mask to your selector like this

jQuery('selector').inputmask("IDR")

下面将提供一个有用的小提琴

A working fiddle is given below

单击以查看小提琴

我已经在chrome浏览器上对此进行了测试,发现可以正常工作.

I have tested this on my chrome browser and found to be working.

为避免出现错误Uncaught SyntaxError:避免使用<input data-inputmask="IDR">,因为在jQuery('selector').inputmask("IDR")命令之前将评估data-inputmask属性.这将导致JSONparse错误: https://github.com/RobinHerbots/Inputmask .

To avoid getting the error Uncaught SyntaxError:Avoid using <input data-inputmask="IDR"> because the data-inputmask attribute will be evaluated before the jQuery('selector').inputmask("IDR") command. This will cause a JSONparse error: https://github.com/RobinHerbots/Inputmask.

这篇关于如何使用jquery应用自定义别名inputmask?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 23:45