如何让 jQuery MaskedInput unmask() 函数正常工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2580550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:50:07  来源:igfitidea点击:

How to get the jQuery MaskedInput unmask() function to work properly?

jqueryjquery-pluginsmaskedtextboxdata-masking

提问by Terry Nederveld

I am trying to have one input on my page that I would like to have a U.S. phone number mask by default. If a end user clicks a checkbox specifing they would like to enter a International phone number I want the mask to be removed.

我试图在我的页面上输入一个默认情况下我想要一个美国电话号码掩码。如果最终用户单击指定他们想要输入国际电话号码的复选框,我希望移除掩码。

I have tried multiple ways and have been unsuccessful thus far. In the current project I am using jQuery to hide/show a completely different input. But I don't like that option and would like a more streamlined approach.

我尝试了多种方法,到目前为止都没有成功。在当前项目中,我使用 jQuery 来隐藏/显示完全不同的输入。但我不喜欢那个选项,想要更精简的方法。

I am using the following:

我正在使用以下内容:

jQuery 1.4.1 (going to upgrade to 1.4.2 soon) and jQuery.MaskedInput-1.2.2

jQuery 1.4.1(即将升级到 1.4.2)和 jQuery.MaskedInput-1.2.2

<script type="text/javascript">
$(document).ready(function($) {
  if ($("#InternationalOfficePhone").attr('checked') == false) {
    $("#OfficePhone").mask("(999) 999-9999? x99999");
  }
});

$("#InternationalOfficePhone").click(function() {
  if ($("#InternationalOfficePhone").attr('checked') == true) {
    //$("#OfficePhone").mask(); //doesn't work
    //$("#OfficePhone").unmask(); //doesn't work
    $("#OfficePhone").unmask("(999) 999-9999? x99999"); //doesn't work
  } else {
    $("#OfficePhone").mask("(999) 999-9999? x99999");
  }
});
</script>

The code above works properly to set the default mask, but no matter what I try on the click event for the InternationalOfficePhone it doesn't remove the mask.

上面的代码可以正常工作以设置默认掩码,但无论我在 InternationalOfficePhone 的单击事件上尝试什么,它都不会删除掩码。

Any help is much appreciated.

任何帮助深表感谢。

回答by Diego Mendes

You don't need to pass a parameter to the unmaskfunction! Use it without a parameter and it works well.

您不需要向unmask函数传递参数!不带参数使用它,效果很好。

$("#OfficePhone").unmask();

回答by Terry Nederveld

I was able to figure out the code to get it to work. Below is the code I am using now.

我能够找出使其工作的代码。下面是我现在使用的代码。

<script type="text/javascript">
$(function($) {
  $("#OfficePhone").mask("(999) 999-9999? x99999");
  $("#InternationalOfficePhone").click(function() {
    var mask = "(999) 999-9999? x99999";
    if ($("#InternationalOfficePhone").is(':checked')) {
      $("#OfficePhone").unmask(mask);
    } else {
      $("#OfficePhone").mask(mask);
    }
  });
});
</script>

回答by Agni

I run through the same problem, and what helped me is:

我遇到了同样的问题,对我有帮助的是:

    var maskedInput = $("#id").data('mask');
    if (maskedInput) {
        maskedInput.remove();
    }

回答by Teja Kantamneni

Try using the :checkedinstead of attrcheck, I think that is the problem. I mean use $('#InternationalOfficePhone').is(':checked')in the if condition.

尝试使用:checked而不是attr检查,我认为这是问题所在。我的意思是$('#InternationalOfficePhone').is(':checked')在 if 条件下使用。

回答by EngineerCoder

Remove the inputmask:

删除输入掩码:

$(selector).inputmask('remove');

or

或者

var input = document.getElementById(selector);
if (input.inputmask)
  input.inputmask.remove()

or

或者

Inputmask.remove(document.getElementById(selector));

回答by user1526780

I am using query.maskedinput.js Version: 1.4.1 I referred https://github.com/digitalBush/jquery.maskedinput/blob/bb66bf9b1c3eddd1d2349cee103127ae08d0a877/demo/index.html

我正在使用 query.maskedinput.js 版本:1.4.1 我提到了 https://github.com/digitalBush/jquery.maskedinput/blob/bb66bf9b1c3eddd1d2349cee103127ae08d0a877/demo/index.html

the unmasking is done by $("input").blur(function() { $("#info").html("Unmasked value: " + $(this).mask());

取消屏蔽是通过 $("input").blur(function() { $("#info").html("Unmasked value: " + $(this).mask());

example Masking is

示例掩码是

$("#pannumber").mask("aaaaa 9999 a");

$("#pannumber").mask("aaaaa 9999 a");

Unmasking

揭开面纱

$("#pannumber").blur(function() {

$("#pannumber").blur(function() {

var pannumber= $(this).mask() ;

var pannumber= $(this).mask() ;