我正在为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没有值
类似帖子:
我的代码出了什么问题?
<小时/> 更新:我已在头部中添加了脚本
答案 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>