问题描述
我尝试在页面上输入一个默认情况下想要使用美国电话号码掩码的输入.如果最终用户单击一个复选框,指定他们想输入国际电话号码,则我希望删除该蒙版.
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的click事件尝试什么,它都不会删除该蒙版.
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()函数正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!