如何让 jQuery MaskedInput unmask() 函数正常工作? [英] How to get the jQuery MaskedInput unmask() function to work properly?

查看:18
本文介绍了如何让 jQuery MaskedInput unmask() 函数正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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 1.4.1(即将升级到 1.4.2)和 jQuery.MaskedInput-1.2.2

jQuery 1.4.1 (going to upgrade to 1.4.2 soon) and 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>

上面的代码可以正常设置默认掩码,但无论我对 InternationalOfficePhone 的点击事件进行什么尝试,它都不会删除掩码.

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.

非常感谢任何帮助.

推荐答案

我能够找出代码让它工作.下面是我现在使用的代码.

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>

这篇关于如何让 jQuery MaskedInput unmask() 函数正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆