jQuery 表单提交后从输入掩码中删除文字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7854651/
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
Remove literals from input mask after form submit?
提问by Wednesday Man
this question has already been asked but the solutions where not clear.
这个问题已经被问到,但解决方案不清楚。
Im using Josh Bush's MaskedInput plugin for jQuery
我在 jQuery 中使用 Josh Bush 的 MaskedInput 插件
What im trying to achieve is:
我试图实现的是:
E.g: a phone input with the mask
例如:带有掩码的电话输入
$("#txtPhone").mask("(99)9999-9999");
EQUALS: (00)9398-8373
等于:(00)9398-8373
i want it to submit : 0093988373
我要提交:0093988373
------ Is it possible to remove the .mask on submit but keep the value?
------ 是否可以在提交时删除 .mask 但保留值?
回答by Alex Peattie
I think you want to use unmask
我想你想用 unmask
$("#myForm").submit(function() {
$("#txtPhone").unmask();
});
回答by Shameem Ahmed
Set removeMaskOnSubmit as true when you initialize the inputmask
初始化 inputmask 时将 removeMaskOnSubmit 设置为 true
$("#txtPhone").inputmask({removeMaskOnSubmit: true});
回答by dperez
回答by Umut Yerebakmaz
unmask is not a function. This is my way of use. autoUnmask: true
unmask 不是一个函数。这是我的使用方式。自动取消掩码:真
$('#amount').inputmask('999.999.999.999', {
numericInput: true,
autoUnmask: true,
});
回答by Ivan
If you are using the completed callback, then you can just use:
如果您使用的是已完成的回调,那么您可以使用:
$(element).mask("(99)9999-9999", {
completed : function () {
var numbers = this.val().replace(/()-/g,'');
}
}
Otherwise, you can use:
否则,您可以使用:
$(element).val().replace(/()-/g,'');
$(element).val().replace(/()-/g,'');
If you want to do this before submitting, I suggesting capturing the submit event, using the code immediately above and then submitting the form.
如果您想在提交之前执行此操作,我建议捕获提交事件,使用上面的代码,然后提交表单。
EDIT:Alex Peattie pointed out the unmask()
function, which I must say is a much better solution than mine. I'll leave my answer here, but I'd go with his solution.
编辑:Alex Peattie 指出了这个unmask()
函数,我必须说这是一个比我的更好的解决方案。我会在这里留下我的答案,但我会选择他的解决方案。
回答by AlexB
You can also extract the raw value on a masked input using
您还可以使用以下方法提取屏蔽输入的原始值
$("#YourSelector").data( $.mask.dataName )();
Source : https://github.com/digitalBush/jquery.maskedinput/issues/318
来源:https: //github.com/digitalBush/jquery.maskedinput/issues/318
Example
If you use a phone
input with such a mask :
示例
如果您使用phone
带有此类掩码的输入:
$(".phone").mask("99 99 99 99 99")
you can extract the user input using :
您可以使用以下方法提取用户输入:
$(".phone").data($.mask.dataName)()
// will produce "0102030405" while the masks displays 01 02 03 04 05
回答by Paulo Henrique
Assuming you have more than just one input being submitted, you could use:
假设您提交的输入不止一个,您可以使用:
// unmask all inputs, before savinf;
$(FormObj+" input[type=text]").each(function() {
$(this).unmask();
});
where FormObj is your form object id, such as #form.
其中 FormObj 是您的表单对象 ID,例如 #form。