【问题标题】:Attaching click event to a table seems not working将点击事件附加到表格似乎不起作用
【发布时间】:2013-06-05 21:57:21
【问题描述】:

我有一个表格,其中包含 2 列中的一些结果和一个复选框。单击复选框时,我应该将单击或选定的复选框放在同一页面的 div 中。

生成的html是这样的:

<table cellspacing="0" cellpadding="4" id="ctl00_PlaceHolderMain_myGrid" style="color:#333333;width:100%;border-collapse:collapse;">
            <tr style="color:White;background-color:#5D7B9D;font-weight:bold;">
                        <th scope="col">&nbsp;</th>
                        <th scope="col">JobCode</th>
                        <th scope="col">JobName</th>
                        <th scope="col">JobPartner</th>
                        <th scope="col">JobManager</th>
                        <th scope="col">ClientName</th>
            </tr>
            <tr style="color:#333333;background-color:#F7F6F3;">
                        <td>
                             <input id="ctl00_PlaceHolderMain_myGrid_ctl02_CheckBox1" type="checkbox" name="ctl00$PlaceHolderMain$myGrid$ctl02$CheckBox1" />
                        </td>
                        <td>Column1</td>
                        <td>Column2</td>
                        <td>Column3</td>
                        <td>Column4</td>
                        <td>Column5</td>
            </tr>
        </table>

在 js 文件中我有这个“:

/// <reference path="jquery-1.9.1.min.js" />
$(document).ready(function () { 


    //On every checkbow that is clicked
    $("#myGrid INPUT").click(function () {

        var clientCode = $(this).parent().parent().parent().find("td:eq(2)").text()
        var clientName = $(this).parent().parent().parent().find("td:eq(1)").text()
        var displayvalue = clientCode.toUpperCase() + " - " + clientName.toUpperCase();
        var removeDiv = $("#" + clientCode);

        removeDiv.remove();

        if ($(this).prop("checked") == true) {
            AddSelectedJob(clientCode, displayvalue);
            // $("[id$=ResultsDiv]").append('<div class="selectedjobs" id=' + clientCode + '>' + displayvalue + '<a href="#"><i class="icon-trash"></i></a></div>');

            //Add to selectedjobs
            FillSelectedJobs();
        }
    });
}

当我使用开发人员工具并附加 js 调试器时,我单击一个复选框,在函数内部有一个断点,没有任何反应。

更新

这是服务器aspx代码

<div style="width: 100%">
        <div style="float: left">
            <asp:Label ID="Label1" runat="server" Text="Search :"></asp:Label>
        </div>
        <div style="float: left">
            <asp:TextBox ID="txtSearch" runat="server" Width="250px"></asp:TextBox>
        </div>
        <div style="float: left">
            <asp:Button ID="btnSearch" runat="server" Text="Search" />
        </div>
        <div style="float: left; margin-left: 20px">
            <asp:Label ID="lblClientCode" runat="server" Text=""></asp:Label>
        </div>
        <div style="clear: both"></div>
        <div>
            <div style="height: 300px; overflow: auto; float: left">
                <asp:GridView ID="myGrid"
                    AutoGenerateColumns="true"
                    runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                    <Columns>
                        <asp:TemplateField>
                            <EditItemTemplate>
                                <asp:CheckBox ID="chkSelected" runat="server" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="CheckBox1" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
            <div style="margin-top: 0px; margin-left: 10px; float: left">
                <asp:Label Text="Selected :" runat="server"></asp:Label>
                <div id="ResultsDiv" style="margin-top: 0px">
                </div>
            </div>
        <div style="clear: both"></div>
    </div>
    <div style="margin-top: 20px; margin-left: 550px">
        <asp:Button ID="btnClose" runat="server" Text="Close" />
    </div>
    <div>
        <asp:Label ID="lblError" runat="server" Text=""></asp:Label>
    </div>
    </div>

【问题讨论】:

  • 哪个元素有id myGrid
  • 我看不到具有提供的 ID 的任何元素。您是遗漏了什么还是您的代码有问题
  • 我已经添加了aspx代码以便更好地理解
  • 你能提供渲染的html代码吗?会更清楚。

标签: javascript jquery


【解决方案1】:

尝试使用 .on()

   $(document).on('click','#myGrid INPUT',function () {

【讨论】:

    【解决方案2】:

    看看这里:http://jsfiddle.net/kb55u/

    使用

    $("#ctl00_PlaceHolderMain_myGrid input").change(function () {
    

    而不是

    $("#myGrid INPUT").click(function () {
    

    那么它应该可以工作

    【讨论】:

      【解决方案3】:

      您的问题是表 id 是由 ASP 分配的,所以当您通过 #myGrid 在 javascript 代码中查找它时,什么也找不到。

      quick solution 会将 asp gridView 包装在带有 id 的 Div 中,并使用该 id 示例:

      <div id="myGridContainer">
      <asp:gridview id="myGrid" ...>
      </div>
      
      $("#myGridContainer INPUT").click(function () {...});
      

      An other solution是选择id以myGrid结尾的元素:

      $("[id$=myGrid] INPUT").click(function () {...});
      

      【讨论】:

      • 如果用户点击了标题行,那么这也会触发吗?
      • 是的,但即使 ASP 没有更改 ID,也会发生这种情况。你想避免这种情况吗?另请注意,ID 可能会更改,因此请勿使用 BeNdErR 的静态硬编码解决方案。
      • 这里是您的 jQuery 的另一个选择器:$("[id$=myGrid] input").click(...);
      【解决方案4】:

      尝试这样做..

      $('#myGrid INPUT').live('click', function(){
      

      【讨论】:

      • 'live' - 与 'bind' 相同 - 将事件附加到控件中。 link
      • 从 jQuery 1.7 开始,不推荐使用 .live() 方法。使用 .on() 附加事件处理程序。请查看the document
      【解决方案5】:

      试试这个:

      $("[id$='CheckBox1']").click(function(){
          alert("CheckBox1 clicked");
      });
      

      【讨论】:

        猜你喜欢
        • 2017-11-08
        • 1970-01-01
        • 2018-11-28
        • 1970-01-01
        • 1970-01-01
        • 2014-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多