【问题标题】:jquery not working on my asp pagejquery 在我的 asp 页面上不起作用
【发布时间】:2011-05-20 08:41:40
【问题描述】:

在过去的几分钟里,我一直在试图找出问题所在..

<%@ 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>

【问题讨论】:

  • 什么是“不工作”? jquery 是否被加载(通过执行 alert('jquery loaded'); 在你的 $(document).ready(function ... >
  • 它是否做了一些意想不到的事情或者代码根本没有执行?它是否适用于最小化的非 ASP HTML 原型?
  • @Raoul @jm_ 在$("#ddModel").change(function () { 工作之前添加警报。但是该函数中的 alert() 不起作用。我是否以错误的方式调用 id?

标签: javascript jquery .net asp.net


【解决方案1】:

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

例如,将CssClass 添加到您的GridViewDropDownList

<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();
        }
    });
});

【讨论】:

  • 这很好用,谢谢!!我只是好奇,jquery中的#有什么用,如果不能这样使用呢?
  • jquery 中的 # 等价于 javascript 中的 document.getElementById。问题出在我们的 asp.net 框架上(它弄乱了 id)。您可以在任何没有 runat="server" 的 html 控件中安全地使用 #。加上 jquery 广泛用于许多其他流行的框架和语言,如 java、php、ruby....
猜你喜欢
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 2016-09-26
相关资源
最近更新 更多