2011-05-20 79 views
0

我一直在试图找出什么不对的,过去几年分钟..jQuery的不工作我的ASP页面

<%@ Page Title="test" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %> 

<%@ PreviousPageType VirtualPath="~/Top.aspx" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"> 
    <style> 
     #pagediv { width: 1500px !important; } 
    </style> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     $("#ddModel").change(function() { 
      var selVal = $(this).find(":selected").text(); 
      var rows = $("#gvTop tr:gt(0)"); 
      alert(selVal); 
      if (selVal == "ALL") { 
       $("#gvTop tr").show(); 
      } 
      else { 
       var rowToShow = rows.find("td:eq(3)").filter(":contains(" + selVal + ")").closest("tr"); 
       rows.show().not(rowToShow).hide(); 
      } 
     }); 
    }); 
</script> 
</asp:Content> 

仍然不知道现在。

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"> 
    <asp:DropDownList ID="ddModel" runat="server" DataSourceID="ddmodelsource" DataTextField="Column1" DataValueField="Column1"> 
    </asp:DropDownList> 
    <asp:GridView ID="gvTop" runat="server" CellPadding="2" CellSpacing="2" GridLines="Vertical"> 
    </asp:GridView> 
</asp:Content> 
+1

什么是“不工作”? jQuery的加载(测试通过做一个警报('jquery加载');在你的$(document).ready(function ... 当你用萤火虫测试页面时,你会看到它被加载或任何JavaScript错误吗? – Raoul 2011-05-20 08:47:05

+0

它会执行一些意想不到的事情,或者代码根本就没有执行?它是否在最小化的非ASP HTML-Prototype中工作? – 2011-05-20 08:48:49

+0

@Raoul @jm_在$(“#ddModel”)之前添加一个警报change(function() {'但是alert()里面的那个函数不起作用,我是用错误的方式调用了id的 – 2011-05-20 08:51:38

回答

3

这是因为在内容页面中,ASP.NET将分配的ID更改为其他内容。如果你查看页面的源代码,你可以看到。因此,替代方案是使用CssClass访问控件。

例如添加CssClassGridViewDropDownList

<asp:DropDownList ID="ddModel" runat="server" DataSourceID="ddmodelsource" DataTextField="Column1" DataValueField="Column1" CssClass="dropdown"> 
</asp:DropDownList> 

<asp:GridView ID="gvTop" runat="server" CellPadding="2" CellSpacing="2" 
     GridLines="Vertical" CssClass="grid"> 
    </asp:GridView> 

现在访问它的jQuery这样。

$(document).ready(function() { 
    $(".dropdown").change(function() { 
     var selVal = $(this).find(":selected").text(); 
     var rows = $(".grid tr:gt(0)"); 
     alert(selVal); 
     if (selVal == "ALL") { 
      $(".grid tr").show(); 
     } 
     else { 
      var rowToShow = rows.find("td:eq(3)").filter(":contains(" + selVal + ")").closest("tr"); 
      rows.show().not(rowToShow).hide(); 
     } 
    }); 
}); 
+0

这很好用,谢谢!!我只是好奇,jquery中有什么用#如果它不能像这样使用?jQuery中的# – 2011-05-20 08:59:18

+1

#在javascript中等价于document.getElementById。问题在于我们的asp.net框架(它让id变混淆了)。您可以安全地在任何html控件中使用#而无需runat =“server”。加上jquery是广泛的y用于许多其他流行的框架和语言,如java,php,ruby .... – naveen 2011-05-20 09:06:08

+0

感谢您的信息伙伴 – 2011-05-20 09:11:55