Jquery选择标记,其中其数据id attr=具有相同数据id的另一个标记


Jquery selecting tag where its data-id attr = another tag with the same data-id

这是我的PHP:

class generate{
// PROJECT TABLE GENERATOR
    public function genTable(){
        global $data;
        global $select;
        global $pdo;

        $result = $data->filterData();
            echo '<table class="table">
                <tbody>';
                echo '<tr>
                        <th>heaer1</th>
                        <th>Header2</th>
                        <th>header3</th>
                        <th>header4</th>
                        <th>header5</th>
                        </tr>

                        <tr class="addForm">
                            <td>
                            <form action="parsers/parseData.php" method="post">
                            <label>Name</label>
                            <input type="text" id="Name">
                            <label>Purpose</label>
                            <input type="text" id="Purpose" >
                            </form>
                            </tr>';
            foreach($result as $res){
            echo '<tr data-id="' . $res['ID'] . '" ><td>' . $res['Name'] .  '</td>
            <td>' . $res['Purpose'] . '</td>
            <td>' . $res['Owner'] . '</td>  
            <td>' . $res['Start'] . '</td>  
            <td>' . $res['End'] . '</td>    
            <td style="width:110px;"><button type="button" id="deleteBtn" class="btn btn-default" name="deleteBtn"><b class="glyphicon glyphicon-trash"></b></button>
            <button type="button" id="editBtn" class="btn btn-default" name="editBtn"><b class="glyphicon glyphicon-pencil"></b></button>
            </td>
            </tr>
            <tr data-id="' . $res['ID'] . '" class="hidden" id="editForm">
            <td>
                <form action="/" method="post">
                    <div class="form-group">
                    <input type="text" id="Name" placeholder="Name">
                    </div>          
            </td>
            <td>
                    <div class="form-group">
                    <input type="text" id="Purpose" placeholder="Purpose">
                    </div>
            </td>
            <td>
            ';
             $select->genSelect();
             echo '             
            </td>
            </form>
            </tr>
            ';
        }
        echo '</form>
    </tbody></table>';
        }

jQuery。。。

$('button#editBtn').click( function(){
var id = $(this).parents('tr').data('id');
var Formid = $(this).closest('tr[id=editForm]').removeClass('hidden');

 console.log(Formid);
});

jQuery不太起作用,我想做的是选择editForm的idtr,它等于它上面的1tr.这样,当我在表中单击它旁边的编辑按钮时,一个窗体会在按钮所在的行下面下拉…这可能吗?

我也尝试过,但它会返回未定义的

    $('button#editBtn').click( function(){
var id = $(this).parents('tr').data('id');
 var attrId = $('tr#editForm').parents().data("id");
console.log(attrId); });

哦,还有这个。。。

$(document).ready(function(e) {
$('tr#editForm').toggle(); });

当我选择正确的标签时,希望我能够再次切换

也许这会对您有所帮助。我不介意仔细检查你的代码,发现错误,因为它有点乱,但我想我几乎明白你的目标是什么。看看:

<? foreach($result as $res) { ?>
    <table>
        <tr id="<?= $res['ID'] ?>">
            <td>Some information</td>
            <td width="10%"><a class="edit">Edit</a></td>
        </tr>
        <tr id="editForm_<?= $res['ID'] ?>" class="hidden">
            <td colspan="2">Edit Form</td>
        </tr>
        <tr id="<?= $res['ID'] ?>">
            <td>Some information</td>
            <td width="10%"><a class="edit">Edit</a></td>
        </tr>
        <tr id="editForm_<?= $res['ID'] ?>" class="hidden">
            <td colspan="2">Edit Form</td>
        </tr>
    </table>
<? } ?>
$('.edit').click(function() {
   var trID = $(this).closest('tr').attr('id');
   $('tr#editForm_'+trID).fadeToggle();
});

SQL FIDDLE演示