2

我的表格行中有一个按钮,它有一个 id btn_number_$i。单击按钮时,我想显示具有 id 的表格行row_$i,该行最初是隐藏的。

这是我创建两个表行的 PHP 代码:

echo "<tr><td>"."<button id= \"btn_number_$i\"  class='btn info toggler' >Info   <i class='icon-arrow-down'></i></button>"."</td></tr>";
echo "<tr id='row_$i' class='info_row'><td>Hello There!!</td></tr>";

下面是我的 jQuery 代码。我正在尝试构建类似命名的行 id 并对其执行.show。但我没有得到想要的结果。

$(function () {
    $('.info_row').hide(); // first I need to hide all the second rows.
    $('.toggler').click(function () {
        var currentId = $(this).attr('id');
        console.log(currentId);
        var lastChar = currentId.substr(currentId.length - 1); //Herein I am trying to extract the last character from btn_number_$i which is $i and then appending it with 'row_'
        var rowId = 'row_' + lastChar;
        console.log(rowId); // I am able to get the current value in console log.
        $('#rowId').show(); // But even after all that i am not able to show the element.
    });
});

任何帮助将不胜感激。

4

1 回答 1

3

您没有使用rowId包含您构造的 id 的变量,而是使用rowId字符串文字。您拥有的选择器将用于查找元素id = rowId

改变

$('#rowId').show();

$('#' + rowId).show();
于 2013-03-12T07:08:46.903 回答