2010-02-28 31 views
3

我試圖掩蓋與2個不同的面具文本框時複選框已經使用jQuery檢查ASP文本框。面膜使用jQuery

我想我與HTML文本框和HTML複選框代碼,它好的工作,但是當我嘗試我的代碼以ASP文本框和ASP複選框沒有反應。

任何建議???

這裏是我的代碼:

<%@ 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" />&nbsp; 

</asp:Content> 

回答

5

當你的控件內的另一個作INamingContainer他們的ID是長於(在你的情況下,至少Content2$chkasp代替chkasp,甚至更長的時間),使用.ClientID來解決這個問題,像這樣:

$(function() { 
    $('#<%=chkasp.ClientID %>').click(function() { 
    if ($(this).is(':checked')) { 
     $("#<%=txtasp.ClientID %>").mask("999-99-9999"); 
    } else { 
     $("#<%=txtasp.ClientID %>").mask("99/99/9999"); 
    } 
}); 

或縮短下來使用的條件表達式:

$('#<%=chkasp.ClientID %>').click(function() { 
    $("#<%=txtasp.ClientID %>").mask($(this).is(':checked') ? 
            "999-99-9999" : "99/99/9999"); 
}); 
+0

謝謝,這幫了我 – SheldonH 2012-06-12 14:05:40