使用 jQuery 在表格中显示/隐藏切换

发布于 2024-07-30 00:38:17 字数 1510 浏览 3 评论 0原文

我正在尝试改编 Andy Langton 的 show/hide/mini-accordion (http://andylangton. co.uk/jquery-show-hide)在表中工作。 我想创建一个事件列表,并为每个事件附加一个确认表。 单击最后一个单元格或行中的“确认”按钮后,我希望显示与此特定事件关联的表单。

Andy 的代码用于

$('.toggle')
    .prev()
    .append('<a href="#" class="toggleLink">'+showText+'</a>');

在隐藏表单之前动态添加切换链接(确认按钮)。 但是,这会在表行中添加链接,而不是在单元格中添加链接。 因此,我已将其更改为“

$('.toggle')
    .prev()
    .append('<td><a href="#" class="toggleLink">'+showText+'</a></td>');

链接现在位于正确的位置,但现在不会调用表单的显示/隐藏”。 当它放置不正确时,尽管不太正确,但功能仍然有效。 我觉得调用切换操作的选择器不正确,但我不知道如何纠正它。 目前

$(this)
    .parent()
    .next('.toggle')
    .toggle('slow');

这基本上就是源代码的样子......

<table id="training-events">
<tr>
   <th>Date / Time</th>
   <th>Event / Venue</th>
   <th>Cost</th>
   <th>Confirm</th>
</tr>
<tr class="event" valign="top">
    <td class="date">Mon, 10 August 2009<br>03:30 PM - 05:30 PM</td>
    <td><h5>Regional Director Meeting</td>
    <td>No Charge</td>
    <td><a href="#" class="toggleLink">Cancel</a></td>
</tr>
<tr style="display: none;" class="toggle">
   <td colspan="4">
      ** FORM **
   </td>
</tr>
</table>

I'm trying to adapt Andy Langton's show/hide/mini-accordion (http://andylangton.co.uk/jquery-show-hide) to work within a table. I'm wanting to create a list of events with a confirmation form attached to each event. Upon clicking on the 'confirm' button in the last cell or the row, I would like the form associated with this particular event to be revealed.

Andy's code uses

$('.toggle')
    .prev()
    .append('<a href="#" class="toggleLink">'+showText+'</a>');

to dynamically add the toggle link (the confirm button) just before the hidden form. This, however, adds the link within the table row and not in a cell. I have therefore changed it to

$('.toggle')
    .prev()
    .append('<td><a href="#" class="toggleLink">'+showText+'</a></td>');

The link is now in the correct place but does now not invoke the show/hide of the form. When it was placed incorrectly the functionality worked, despite not being quite right. I feel that the selector calling the toggle action is not correct but I don't know how to correct it. It is currently

$(this)
    .parent()
    .next('.toggle')
    .toggle('slow');

This is essentially how the source looks...

<table id="training-events">
<tr>
   <th>Date / Time</th>
   <th>Event / Venue</th>
   <th>Cost</th>
   <th>Confirm</th>
</tr>
<tr class="event" valign="top">
    <td class="date">Mon, 10 August 2009<br>03:30 PM - 05:30 PM</td>
    <td><h5>Regional Director Meeting</td>
    <td>No Charge</td>
    <td><a href="#" class="toggleLink">Cancel</a></td>
</tr>
<tr style="display: none;" class="toggle">
   <td colspan="4">
      ** FORM **
   </td>
</tr>
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘴硬脾气大 2024-08-06 00:38:17

你需要这样的东西:

$(this).parent().parent().next('.toggle').toggle('slow')

或者

$(this).closest('tr').next('.toggle').toggle('slow'); 

你只进行一次parent()调用,它会把你带到TD,你需要进入TR。

You need something like:

$(this).parent().parent().next('.toggle').toggle('slow')

or

$(this).closest('tr').next('.toggle').toggle('slow'); 

You're only making a single parent() call which brings you to the TD, you need to step up to the TR.

高速公鹿 2024-08-06 00:38:17

如果我删除 style="display:none;" 这对我有用 从切换TR。

您会在示例中注意到他也在做的事情:

 $('.toggle').hide();

这就是隐藏切换类元素的原因。

This works for me if I remove the style="display:none;" from the toggle TR.

You'll notice in the example he's also doing:

 $('.toggle').hide();

This is what is hiding the toggle classed elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文