使用jQuery的asp文本框的掩码 [英] mask for asp text box using jquery
问题描述
当使用jquery选中一个复选框时,我正在尝试使用2种不同的蒙版对文本框进行蒙版.
I'm trying to mask text box with 2 different masks when a check box has been checked using jquery.
我尝试了使用html文本框和html复选框的代码,并且工作正常,但是当我尝试使用asp文本框和asp复选框的代码时没有响应.
I tried my code with html text box and html check box and it is working ok but when I tried my code with asp text box and asp check box there is no response.
任何建议?
这是我的代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Imam.Master" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="Imam_Contacts.WebForm4" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script src="js/jquery-1.4.1.js" type="text/javascript"></script>
<script src="js/jquery.maskedinput-1.2.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(
function() {
$('#chkhtml').click(
function() {
if ($('#chkhtml:checked').length > 0) {
$("#txthtml").mask("999-99-9999");
} else {
$("#txthtml").mask("99/99/9999");
}
});
});
$(document).ready(
function() {
$('#chkasp').click(
function() {
if ($('#chkasp:checked').length > 0) {
$("#txtasp").mask("999-99-9999");
} else {
$("#txtasp").mask("99/99/9999");
}
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:CheckBox ID="chkasp" runat="server" />
asp:<asp:TextBox ID="txtasp" runat="server"></asp:TextBox>
<input id="chkhtml" type="checkbox" checked="checked" title="chkhtml" />HTML <input id="txthtml" type="text" />
</asp:Content>
推荐答案
当您的控件位于另一个INamingContainer中时,其ID会比该ID更长(在您的情况下,至少为Content2$chkasp
而不是chkasp
,也许更长),请使用.ClientID
来解决此问题,如下所示:
When your controls are inside another INamingContainer their IDs are longer than that (In your case at least Content2$chkasp
instead of chkasp
, maybe longer), use .ClientID
to resolve this, like this:
$(function() {
$('#<%=chkasp.ClientID %>').click(function() {
if ($(this).is(':checked')) {
$("#<%=txtasp.ClientID %>").mask("999-99-9999");
} else {
$("#<%=txtasp.ClientID %>").mask("99/99/9999");
}
});
或使用条件表达式将其缩短:
Or shorten it down using a conditional expression:
$('#<%=chkasp.ClientID %>').click(function() {
$("#<%=txtasp.ClientID %>").mask($(this).is(':checked') ?
"999-99-9999" : "99/99/9999");
});
这篇关于使用jQuery的asp文本框的掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!