Jquery过滤器()不起作用

时间:2014-04-17 04:06:23

标签: jquery asp.net gridview

我正在为GridView使用简单的过滤功能 (我从教程中复制粘贴的精确代码并使用sharpLinter检查语法..)

的JavaScript

$(document).ready(function () {
    $("#<%=grdRows.ClientID%> tr")
            .filter(":odd")
            .css("background-color", "grey");
});

主管科

<head runat="server">

    <script src="jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="submit.js" type="text/javascript"></script>
</head>

GridView标记

 <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grdRows" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="Id" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" 
                    InsertVisible="False" 
                    ReadOnly="True" 
                    SortExpression="Id" />
                <asp:BoundField DataField="FUllName" HeaderText="FUllName" 
                    SortExpression="FUllName" />
                <asp:BoundField DataField="Gender" HeaderText="Gender" 
                    SortExpression="Gender" />
            </Columns>
        </asp:GridView>

Firebug向我显示此JavaScript错误

  

TypeError:$不是函数

当我console.log时grdRows.ClientID没有值

类似帖子:

  1. Jquery selectors - filtering ASP.NET gridview(这是关于网格中的复选框)
  2. Complex jQuery filter() not working
  3. 我的代码出了什么问题?

    <小时/> 更新:我已在头部

    中添加了脚本

2 个答案:

答案 0 :(得分:1)

您的代码中存在许多潜在问题/不必要的事情。首先要做的事情。

  

TypeError:$不是函数

表示您尚未添加reference to the jQuery library,或者您conflict with another library使用$用于其他目的。

<小时/> 一旦你解决了这个错误,让我们回到你的代码。我们在这里使用asp:GridView <AlternatingRowStyle BackColor="Gray"/> 。而不是那个jQuery过滤器,你只需要在标记

中写这个
:odd

现在,如果您想为<AlternatingRowStyle CssClass="grdRow_odd"/> 行使用一个类,请执行此操作

$("#<%=grdRows.ClientID%> tr.grdRow_odd")
        .css("background-color", "grey");

现在你的JavaScript成了,

$(".grdRow_odd").css("background-color", "grey");

或简化

{{1}}

无论您使用哪种框架,请始终记住框架是您的朋友

答案 1 :(得分:0)

TypeError: $ is not a function表示您不包含jQuery文件

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>