Jquery, ajax:单击按钮并获取其旁边的单元格在同一行中的值

Jquery, ajax: Clicking on a button and getting the value of the cell next to it on the same row

本文关键字:单元格 一行 单击 ajax 按钮 获取 Jquery      更新时间:2023-09-26

我一直在阅读关于JS的教程,它发生在我身上,我想要以下内容:

假设一个表有3个颜色和几行。

第一个冒号是int值,第二个冒号是绿色按钮,第三个是红色按钮

在单击按钮时,它应该得到第一列的单元格的值(我正在做一个轮询表)。

更新四:

这终于工作了,感谢Adeneo和Anthony Grist。谢谢您的耐心和帮助。

在这里:

<script>
    $(document).ready(function(){
        $('.oui').click(function(){
            var valor = $(this).closest('tr').find('td:first').text();
            $(this).attr('value', valor);
        });
    });
</script>
HTML:

<div class="container">
    <!-- Example row of columns -->
    <div class="row">
        <div class="col-lg-12">
            <table class="table table-striped table-bordered">
                <thead>
                    <tr>
                        <th>Prenom</th>
                        <th>Nom</th>
                        <th>Tantieme</th>
                        <th>Oui</th>    
                        <th>Non</th>
                        <th>Abstention</th>    
                        <th>Absent</th>
                    </tr>
                </thead>
                <tbody>
                @foreach ($polls as $vote)
                    <tr>
                        <td>{{ $vote->prenom}}</td>
                        <td>{{ $vote->nom}}</td>
                        <td>{{ $vote->tantieme }}</td>
                        <td><button type="button" id="oui" class="btn btn-success">VOTE OUI</button></td>    
                        <td><button type="button" id="non" class="btn btn-danger">VOTE NON</button></td>
                        <td><button type="button" class="btn btn-info">ABSTENTION</button></td>    
                        <td><button type="button" id="default" value = "230" class="btn btn-default">ABSENT</button></td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>

我一直在阅读关于JS的教程,它发生在我身上,我想要以下内容:

假设一个表有3个颜色和几行。

第一个冒号是int值,第二个冒号是绿色按钮,第三个是红色按钮

在单击按钮时,它应该得到第一列的单元格的值(我正在做一个轮询表)。

更新四:

这终于工作了,感谢Adeneo和Anthony Grist。谢谢您的耐心和帮助。

在这里:

<script>
    $(document).ready(function(){
        $('.oui').click(function(){
            var valor = $(this).closest('tr').find('td:first').text();
            $(this).attr('value', valor);
        });
    });
</script>
HTML:

<div class="container">
    <!-- Example row of columns -->
    <div class="row">
        <div class="col-lg-12">
            <table class="table table-striped table-bordered">
                <thead>
                    <tr>
                        <th>Prenom</th>
                        <th>Nom</th>
                        <th>Tantieme</th>
                        <th>Oui</th>    
                        <th>Non</th>
                        <th>Abstention</th>    
                        <th>Absent</th>
                    </tr>
                </thead>
                <tbody>
                @foreach ($polls as $vote)
                    <tr>
                        <td>{{ $vote->prenom}}</td>
                        <td>{{ $vote->nom}}</td>
                        <td>{{ $vote->tantieme }}</td>
                        <td><button type="button" id="oui" class="btn btn-success">VOTE OUI</button></td>    
                        <td><button type="button" id="non" class="btn btn-danger">VOTE NON</button></td>
                        <td><button type="button" class="btn btn-info">ABSTENTION</button></td>    
                        <td><button type="button" id="default" value = "230" class="btn btn-default">ABSENT</button></td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>