简体   繁体   English

使用asp.net的jQuery输入掩码覆盖文本框值

[英]jquery input mask overwriting textbox value with asp.net

I have an ASP.NET web form, and I'm using the masked input plugin from http://digitalbush.com/projects/masked-input-plugin , which seems to be a pretty popular plugin. 我有一个ASP.NET Web表单,并且正在使用http://digitalbush.com/projects/masked-input-plugin中的masked输入插件,这似乎是一个非常流行的插件。 It works very well, but I'm having an issue when attempting to pre-populate a value in a textbox using the masking. 它工作得很好,但是尝试使用遮罩在文本框中预填充值时遇到问题。

In my document.ready, Im performing: 在我的document.ready中,我正在执行以下操作:

$("#txtPrimaryPhone").mask("(999) 999-9999 ?x9999");

In my codebehind I set my text property like so: 在我的后台代码中,我将text属性设置如下:

txtPrimaryPhone.Text = "1234567890";

When my page loads, my textbox is empty. 页面加载后,文本框为空。 Has anyone run into similar issues, or can anyone offer any suggestions? 是否有人遇到类似问题,或者有人可以提供任何建议? Thanks! 谢谢!

Here's the solution : 这是解决方案:

Instead of 代替

$("#txtPrimaryPhone").mask("(999) 999-9999 ?x9999");

Try this 尝试这个

 $.each($('#txtPrimaryPhone'), function (iEmt, emt) {
            $(emt).mask("(999) 999-9999 ?x9999").val($(emt).attr("value"));
        });

it works for me :) 这个对我有用 :)

If I'm correct, the jQuery mask is applied once you load your page, then your txtPrimaryPhone.Text is set server side. 如果我是对的,则在加载页面后将应用jQuery掩码,然后将txtPrimaryPhone.Text设置为服务器端。 I have two theories. 我有两种理论。

One : 1234567890 is not accepted by the mask because it is not in the right format. 一个 :掩码不接受1234567890,因为它的格式不正确。 Right format should be something like txtPrimaryPhone.Text = “(999) 999-9999 x99999”;. 正确的格式应类似于txtPrimaryPhone.Text =“(999)999-9999 x99999”;。

Two : The mask works as a filter for user input but does not accept the server side value for txtPrimaryPhone. :掩码用作用户输入的过滤器,但不接受txtPrimaryPhone的服务器端值。 It's a little hard to find out without a sample we can debug or edit. 如果没有我们可以调试或编辑的示例,很难找到答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM