【问题标题】:Table with several toggle triggers needing to be controlled by one piece of jQuery带有多个切换触发器的表需要由一个 jQuery 控制
【发布时间】:2012-12-18 19:39:22
【问题描述】:

我有一个我正在 ASP.NET 中处理的表,其中每个主行都有 2 个隐藏行,这些行用于在文本框中添加 cmets 或使用第二个隐藏行中的列表框来填充文本框做了cmets。将有未知数量的行,因为它基于我们数据库中每个客户端的数据。在主行的最后一列中有一个图像,它是单击触发器,用于显示隐藏在其后面的行。由于某种原因,我正在使用的 click evt 无法正常工作。任何帮助都会很棒。

注意:我还使用 for 循环来计算我的点击事件发生的位置,当表格完成时,for 循环将从后面的代码中的计数器运行,因此“

HTML:

  <table id="mfCriteriaTable">
                <tr class="whiteBack mfTableBorder">
                    <td class="mfRightCol showCell" width="140px" align="left">Category</td>
                    <td class="colOne" width="600px" align="left">Criteria for achieving this element of service quality is listed here. Additional Detail click info</td>
                    <td width="50px" align="center"><asp:RadioButton ID="mfScoreRad1" runat="server" /></td>
                    <td width="50px" align="center"><asp:CheckBox ID="mfNA1" runat="server" /></td>
                    <td width="50px" align="center"><asp:CheckBox ID="mfND1" runat="server" /></td> 
                    <td width="75px" align="center"><a href="#" id="mfComments1"><img src="Images/blue_info_tag.png" alt="" /></a></td>
                </tr>
                <tr class="whiteBack" id="commentRow1" style="display:none;"><td colspan="6" id="tdComment1" height="30px"><asp:TextBox ID="txtComments1" CssClass="mfTextComments" runat="server" TextMode="MultiLine"></asp:TextBox></td></tr>
                <tr class="whiteBack" id="slTableComments1" style="display:none;">
                     <td colspan="6" height="30px">
                        <asp:ListBox ID="lbComments1" runat="server" Height="20px" ToolTip="Please select a comment from this list or create your own in the text area above.">
                            <asp:ListItem Value="Comment 1">This is where comment number one will be, this is a longer comment just to see how it will all fit together.</asp:ListItem>
                            <asp:ListItem Value="Comment 2">This is a 2nd comment that is a bit shorter but shows usability. </asp:ListItem>
                            <asp:ListItem Value="Comment 3">And this is a 3rd comment so you are almost done.</asp:ListItem>
                        </asp:ListBox>
                    </td> 
                </tr>
                <tr class="whiteBack mfTableBorder">
                    <td class="mfRightCol showCell" width="140px" align="left">Category</td>
                    <td class="colOne" width="600px" align="left">Criteria for achieving this element of service quality is listed here. Additional Detail click info</td>
                    <td width="50px" align="center"><asp:RadioButton ID="mfScoreRad2" runat="server" /></td>
                    <td width="50px" align="center"><asp:CheckBox ID="mfNA2" runat="server" /></td>
                    <td width="50px" align="center"><asp:CheckBox ID="mfND2" runat="server" /></td> 
                    <td width="75px" align="center"><a href="#" id="mfComments2"><img src="Images/blue_info_tag.png" alt="" /></a></td>
                </tr>
                <tr class="whiteBack" id="commentRow2" style="display:none;"><td colspan="6" id="tdComment2" height="30px"><asp:TextBox ID="txtComments2" CssClass="mfTextComments" runat="server" TextMode="MultiLine"></asp:TextBox></td></tr>
                <tr class="whiteBack" id="slTableComments2" style="display:none;">
                     <td colspan="6" height="30px">
                        <asp:ListBox ID="lbComments2" runat="server" Height="20px" ToolTip="Please select a comment from this list or create your own in the text area above.">
                            <asp:ListItem Value="Comment 1">This is where comment number one will be, this is a longer comment just to see how it will all fit together.</asp:ListItem>
                            <asp:ListItem Value="Comment 2">This is a 2nd comment that is a bit shorter but shows usability. </asp:ListItem>
                            <asp:ListItem Value="Comment 3">And this is a 3rd comment so you are almost done.</asp:ListItem>
                        </asp:ListBox>
                    </td> 
                </tr>
            </table>

Javascript:

     function assignClickHandlerFor(num) {

            window.console && console.log('#mfComments' + num);
            $('#mfComments' + num).click(function (evt) {
                evt.preventDefault();
                evt.stopPropagation();

                var $aBox = $(evt.currentTarget); 

                $aBox.find('tr#commentRow' + num).toggle;
                $aBox.find('tr#slTableComments' + num).toggle;
            });
    }

    var i;

    for (i = 1; i <= 7; i++) {
        assignClickHandlerFor(i);
    }

【问题讨论】:

  • 在您提供的代码中,永远不会调用函数 assignClickHandlerFor(),因此永远不会定义点击事件。
  • @Lokase 格式很乱,看起来就是这样。它在循环中被调用。我为他重新格式化了。

标签: jquery triggers html-table toggle show-hide


【解决方案1】:

为了使选择更容易,请将所有可点击的图片提供class="mfComments",然后:

$(function() {
    $('.mfComments').click(function (evt) {
        evt.preventDefault();
        evt.stopPropagation();
        $(this).closest("tr").next("tr").next("tr").andSelf().toggle();
    });
});

我不确定循环计数器业务。我不明白它应该达到什么目的。

【讨论】:

  • 我认为andSelf 不应该在那里。我没有看到他在哪里切换主行。
  • @JamesMontagne,也许我误读了这个问题......我会回去再读一遍......
  • James 是对的,我不需要切换第一行,因为它总是可见的。
  • 我觉得还可以。当表达式到达第二个.next("tr") 时,andSelf() 将它与第一个.next("tr") 合并。因此接下来的两个 `' 元素被选中。第一行不在选择中,不会被切换。
  • 感谢 Beetroot 提供的信息,它对 JQ 新手非常有用。
【解决方案2】:

toggle 是一个方法,因此在它之后需要()。此外,find 看起来 一个元素中。您不想查看您的td

$('tr#commentRow' + num).toggle();
$('tr#slTableComments' + num).toggle();

您也可以考虑在这些元素上放置类,然后使用closestprevnext 等来定位适当的元素,而不是这种递增ids 的方法。它需要重新编写,但可能会更干净一些。

【讨论】:

  • 啊,谢谢你的错字,不知道为什么我忘记了 ()... 至于最接近和下一个,他们似乎不想在寻找 2 个单独的项目时工作,但我会尝试返工。
  • @BobbyFarrow Beetroot 的回答大致就是我想要的。
猜你喜欢
相关资源
最近更新 更多
热门标签