触发的AutoPostBack =真jQuery的code? [英] Trigger jQuery code on AutoPostBack = true?
问题描述
我有一点jQuery的code的适用的电话号码掩码的文本字段的形式:
<脚本类型=文/ JavaScript的SRC =../../../ JS / jquery.maskedinput.min.js>< / SCRIPT&GT ;<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
$ .mask.definitions ['〜'] ='[+ - ]';
$('电话')面膜('(999)999-9999')。
});
< / SCRIPT>
当用户第一次进入的形式,或者如果使用刷新页面它工作得很好。然而,有一个DropDownList,将允许用户选择不同的送货地址。这DropDownList的有AutoPostBack属性设置为true。
< TR ID =trAddressBook=服务器>
<第i运送至:< /第i
&所述; TD>
< ASP:DropDownList的ID =通讯簿=服务器WIDTH =200像素DataTextField =值DataValueField =密钥AppendDataBoundItems =真的AutoPostBack =真>
< ASP:ListItem的文本=帐单地址VALUE =0/>
< ASP:ListItem的文本=新地址VALUE = - 1/>
< / ASP:DropDownList的>
< / TD>
< / TR>
每当用户选择此DropDownList中的一个选项,手机屏蔽jQuery的code不再出现在电话号码文本框(ES)。我看不出有什么错误,当我检查谷歌浏览器的页面。
有什么方法来维持手机屏蔽code无论用户是否使用DropDownList的或不?
下面是从后面code在Page_Load功能:
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
//确定出货形式必须初始化
布尔initShipAddress;
如果(Page.IsPostBack)
{
//在回发,我们只初始化运输形式,如果地址选择改变
initShipAddress =(的Request.Form [__ EVENTTARGET] == AddressBook.UniqueID);
}
其他
{
//在第一次访问,我们初始化出货形式如果装运地址不开票
initShipAddress =(GetShippingAddressId()= _User.PrimaryAddress.Id!);
}
如果(initShipAddress)InitializeShippingAddress();
}
我可能是错的,因为它是以来我和ASP.NET工作了很长一段时间,但我相信你可以简单地改变 $(文件)。就绪(函数(){...});
到 $(窗口).load(函数(){...});
$(窗口).load(函数(){
$ .mask.definitions ['〜'] ='[+ - ]';
$('电话')面膜('(999)999-9999')。
});
如果做不到这一点,ASP.NET执行调用的函数页面加载
每次页面加载:
功能页面加载(){
$ .mask.definitions ['〜'] ='[+ - ]';
$('电话')面膜('(999)999-9999')。
};
I have a bit of jQuery code that applies a phone number mask to a text field in a form:
<script type="text/javascript" src="../../../js/jquery.maskedinput.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.mask.definitions['~'] = '[+-]';
$('.phone').mask('(999) 999-9999');
});
</script>
It works just fine when the user first enters the form or if the use refreshes the page. However, there is a DropDownList that will allow users to select a different shipping address. This DropDownList has its AutoPostBack Property set to "true."
<tr id="trAddressBook" runat="server">
<th>Ship To:</th>
<td>
<asp:DropDownList ID="AddressBook" runat="server" Width="200px" DataTextField="Value" DataValueField="Key" AppendDataBoundItems="true" AutoPostBack="true">
<asp:ListItem Text="Billing Address" Value="0" />
<asp:ListItem Text="New Address" Value="-1" />
</asp:DropDownList>
</td>
</tr>
Whenever the user select an option from this DropDownList, the phone mask jQuery code no longer appears in the phone number textbox(es). I don't see any errors when I inspect the page in Google Chrome.
Is there someway to maintain the phone mask code regardless of whether the user uses the DropDownList or not?
Here is the Page_Load function from the code behind:
protected void Page_Load(object sender, EventArgs e)
{
// DETERMINE IF THE SHIPPING FORM MUST BE INITIALIZED
bool initShipAddress;
if (Page.IsPostBack)
{
// ON POSTBACK, WE ONLY INITIALIZE THE SHIPPING FORM IF THE ADDRESS SELECTION WAS CHANGED
initShipAddress = (Request.Form["__EVENTTARGET"] == AddressBook.UniqueID);
}
else
{
// ON FIRST VISIT, WE INITIALIZE SHIPPING FORM IF THE SHIPPING ADDRESS IS NOT THE BILLING
initShipAddress = (GetShippingAddressId() != _User.PrimaryAddress.Id);
}
if (initShipAddress) InitializeShippingAddress();
}
I may be wrong as it's been a long time since I worked with ASP.NET, but I believe you can simply change $(document).ready(function () { ... });
to $(window).load(function() { ... });
$(window).load(function () {
$.mask.definitions['~'] = '[+-]';
$('.phone').mask('(999) 999-9999');
});
Failing that, ASP.NET executes a function called pageLoad
every time a page loads:
function pageLoad() {
$.mask.definitions['~'] = '[+-]';
$('.phone').mask('(999) 999-9999');
};
这篇关于触发的AutoPostBack =真jQuery的code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!