javascript - Jquery只激活当前行按钮

标签 javascript jquery

我有 2 个按钮来扩展和缩小特定行的子表。单击时,按钮将从 + 变为 -。目前,当我单击按钮时,会显示正确的行。然而,所有按钮都变成了-。我可以知道如何将按钮限制为仅当前行。

这是我的代码

$('.expandlink').on('click', function () {
    var curr_row = $(this).parent().parent('tr').attr('data-id');

    $(".subtable").each(function () { //loop through each row
        if ($("[type='hidden']", this).val() == curr_row) {
            $(this).show();
            $('.expandlink').hide();
            $('.shrinklink').show();
        }
    });
});

$('.shrinklink').on('click', function () {
    var curr_row = $(this).parent().parent('tr').attr('data-id');

    $(".subtable").each(function () {
        if ($("[type='hidden']", this).val() == curr_row) {
            $(this).hide();
            $('.shrinklink').hide();
            $('.expandlink').show();
        }
    });
});
<tbody>
<?php $offset = $this->uri->segment(4,0)+1; ?>
<?php foreach($user as $row): ?>
<tr data-id="<?php echo $row->id; ?>">
<td><?php echo $offset++; ?></td>
<td><?php echo $row->company; ?></td>
<td><?php echo $row->firstname; ?></td>
<td><?php echo $row->lastname; ?></td>
<td><input type="hidden" class="id" name="id" value="<?php echo $row->id; ? >"/><input type="button" class="expandlink" value="+"/></td>
<td><input type="hidden" class="id" name="id" value="<?php echo $row->id; ?>"/><input type="button" class="shrinklink" value="-"/></td>
<td><input type="hidden" class="id" name="id" value="<?php echo $row->id; ?>"/><input type="hidden" name="company" value="<?php echo $row->id; ?>"/><input type="hidden" name="set" value="edit"/><input type="button" class="editlink" value="Edit"/></td>
<td><input type="hidden" class="id" name="id" value="<?php echo $row->id; ?>"/> <input type="hidden" name="company" value="<?php echo $row->id; ?>"/><input type="hidden" name="set" value="delete"/><input type="button" class="deletelink"  value="Delete"/></td>
    </tr>
    <tr class="subtable">
    <td><input type="hidden" value="<?php echo $row->id; ?>"/></td>
    <td><?php echo $row->company; ?></td>
    <td><?php echo $row->firstname; ?></td>
    <td><?php echo $row->lastname; ?></td>
</tr>
<?php endforeach; ?>
</tbody>

最佳答案

首先找到父行,然后指定其子行

$(this).parents("tr").find('.expandlink').hide();
$(this).parents("tr").find('.shrinklink').show();

编辑: 阅读您的表结构后,只注意到子表位于不同的 <tr> 中标签

$('.expandlink').on('click', function () {
    var curr_row = $(this).parent().parent('tr').attr('data-id');
    var row = $(this).parents("tr");
    $(".subtable").each(function () { //loop through each row
        if ($("[type='hidden']", this).val() == curr_row) {
            $(this).show();
            row.find('.expandlink').hide();
            row.find('.shrinklink').show();
        }
    });
});

关于javascript - Jquery只激活当前行按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34829243/

相关文章:

javascript - 如何使用 JavaScript 检测是否一次按下多个键?

java - 前端和后端的电子邮件验证解决方案

javascript - jQuery if 条件文本包含

jquery - 使用 jQuery 在 Android 上确定长按(长按,点击保持)

javascript - 在 Angular 2 项目中添加 .js 脚本

javascript |使用正则表达式替换字符串

javascript - 使用 javascript/jquery 删除字符串的开头

javascript - 在 html2canvas 事件中设置隐藏字段的值

jquery - 如何更改圆环图中标签的位置?

javascript - php 在弹出的 div 中发布内容