Javascript无法在内容中使用

时间:2013-07-01 06:53:41

标签: c# javascript jquery asp.net

我有javascript在表单中工作,但是当我试图把它放在内容上时不再有效..

这是有效的javascript:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Numeric.aspx.cs" Inherits="Numeric" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
        <script type="text/javascript" src="Scripts/jquery.numeric.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <form>
        Numbers only:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <input class="numeric" type="text" />
        <script type="text/javascript">
            $("#TextBox1").numeric();
        </script>
    </form>
    </div>
    </form>
</body>
</html>

这里的内容不适用于javascript:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="NumericNew.aspx.cs" Inherits="NumericNew" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
 <script type="text/javascript" src="Scripts/jquery.numeric.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
 <asp:ScriptManager ID="ScriptManager1" runat="server" > </asp:ScriptManager>
 <div>

        Numbers only:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <input class="numeric" type="text" />
        <script type="text/javascript">
            $("#TextBox1").numeric();   
        </script>
</div>
</asp:Content>

请让我知道......谢谢。

1 个答案:

答案 0 :(得分:3)

因为您使用的是母版页,所以控件ID不会保留TextBox1

使用$("#<%= TextBox1.ClientID %>")

如果你签入了firebug或其他工具,你会发现id就像ctl00_TextBox1