简体   繁体   中英

Get value from HTML Table Cell

I want a HTML Table in such a way that I can select only one cell and retrieve it's value so I can use in other operations on my app. The problem is that I have no idea on how to structure the table in a way that only one cell can be selected, the selected cell change it's color and so on. Here's an example so you guys can picture what I have in mind.

Table selection

And here is the hardcoded model of my app:

<body>
    <!-- NAVBAR INÍCIO -->
    <nav id="barra_navegacao"class="navbar navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="jogos.html" title="Início">
                    <i class="fa fa-home"></i>
                </a>

                <a class="navbar-brand" href="jogos.html" title="Início">
                    <i class="fa fa-bluetooth"></i>
                </a>

                <a class="navbar-brand" href="jogos.html" title="Início">
                    <i class="fa fa-print"></i>
                </a>

                <a class="navbar-brand" href="jogos.html" title="Início">
                    <i class="fa fa-whatsapp"></i>
                </a>

            </div>
            <div id="navbar" class="navbar-collapse collapse">

                <!--<ul class="nav navbar-nav">
                    <li><a href=""><span class="fa glyphicon-log-out"></span> <small>SAIR</small></a></li>
                </ul> 
                <ul class="nav navbar-nav">
                    <li><a style="color: red;" href="../config/encerra_acesso.php"><span class="glyphicon glyphicon-log-out"></span> <small>SAIR</small></a></li>
                </ul> /-->                   

            </div>
        </div>
    </nav>
    <!-- NAVBAR FIM-->



    <!-- PAINEL APOSTAS INÍCIO -->
    <div id="painel_aposta" class="painel-aposta row">
        <div class="col-xs-6">
            <div class="input-group">
                <span class="input-group-addon" id="add-on">R$</span>
                <input style="width: 90px;" type="text" class="form-control" placeholder="5.00" aria-describedby="add-on">
            </div>
            Retorno(R$): <b>0.00</b><br>
            Qtd. Jogos: <b>0</b><br>
        </div>
        <div class="pull-right">
            <button id="finalizar_aposta" type="button" class="btn btn-info btn-sm" onclick="">
                <i class="fa fa-refresh"></i>
                Atualizar
            </button>

            <button id="finalizar_aposta" class="btn btn-success btn-sm" 
                onclick="submitForm('form_apostas')">
            <i class="fa fa-check"></i>
            Finalizar
            </button>
        </div>
    </div>
    <!-- PAINEL APOSTAS FIM -->




    <!-- COTAÇÕES INÍCIO -->
    <div id="painel_partidas" class="panel panel-primary panel-heading-margin">
        <div class="panel-heading">
            <center>
                <b>Brasil &raquo; Série A</b>
                <button data-toggle="collapse" data-target="#partida" class="btn btn-default btn-xs pull-right"><i class="fa fa-compress"></i></button>
            </center>
        </div>

        <div id="partida" class="panel-heading-margin">


            <table class="w3-table-all w3-card-4" style="width: 100%">
                <tr class="w3-dark-grey">
                    <td>
                        <small>TALLERES ESCALADA x EXCURSIONISTAS</small>
                        <span class="pull-right"><small>08/10 18:00 &nbsp; </small> <button class="btn btn-danger btn-xs pull-right"><span class="fa fa-close"></span></button></span>
                    </td>
                </tr>
            </table>

            <table class="w3-table-all w3-card-4" style="width: 100%">

                <tbody>
                    <tr>
                        <td>
                            <center>
                                Casa<br>
                                <strong>2.00</strong>
                            </center>
                        </td>
                        <td>
                            <center>
                                Casa<br>
                                <strong>2.00</strong>
                            </center>
                        </td>
                        <td>
                            <center>
                                Casa<br>
                                <strong>2.00</strong>
                            </center>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <center>
                                Casa<br>
                                <strong>2.00</strong>
                            </center>
                        </td>
                        <td>
                            <center>
                                Casa<br>
                                <strong>2.00</strong>
                            </center>
                        </td>
                        <td>
                            <center>
                                Casa<br>
                                <strong>2.00</strong>
                            </center>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <center>
                                Casa<br>
                                <strong>2.00</strong>
                            </center>
                        </td>
                        <td>
                            <center>
                                Casa<br>
                                <strong>2.00</strong>
                            </center>
                        </td>
                        <td>
                            <center>
                                Casa<br>
                                <strong>2.00</strong>
                            </center>
                        </td>
                    </tr>
                </tbody>

            </table>

            <div id="resultado"></div>

                <!--COTAÇÕES AQUI-->

        </div>

    </div>
    <!-- COTAÇÕES FIM -->


    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="assets/js/webservice.js"></script>
    <script type="text/javascript" src="assets/js/index.js"></script>
    <script type="text/javascript" src="assets/pace/pace.min.js"></script>
    <script type="text/javascript" src="assets/js/jquery-2.2.4.min.js"></script>
    <script type="text/javascript" src="assets/bootstrap/js/bootstrap.min.js"></script>
    <!--<script type="text/javascript">listaCampeonatos();</script>-->
    <!--<script type="text/javascript" src="assets/materialize/js/materialize.min.js"></script>-->

</body>

My app

I need basically the same thing in the fist screenshot. If someone can enlighten me with some help I'd appreciate. Thanks in advance.

give an id and use getElementById() to select the cell. Don't use the center tag, its depricated. I am assuming you want to use javascript

try like this. i am giving the value in alert. you can just assign the value in a variable and use in your app. the html table is full yours one, i just added an id to the table. and a css class 'active' to hightlight the click div. (you need to include the jquery script in you page)

 $(document).ready(function(){ $(document).on('click','.mytable tbody tr td',function(){ $(this).addClass('active'); $(this).parents('tbody').find('td').not(this).removeClass('active'); alert($(this).find('strong').html()); }); }); 
 .active{ background: #999; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="" border='1' class="mytable w3-table-all w3-card-4" style="width: 100%"> <tbody> <tr> <td> <center> Casa<br> <strong>2.00</strong> </center> </td> <td> <center> Casa<br> <strong>2.00</strong> </center> </td> <td> <center> Casa<br> <strong>2.00</strong> </center> </td> </tr> <tr> <td> <center> Casa<br> <strong>2.00</strong> </center> </td> <td> <center> Casa<br> <strong>2.00</strong> </center> </td> <td> <center> Casa<br> <strong>2.00</strong> </center> </td> </tr> <tr> <td> <center> Casa<br> <strong>2.00</strong> </center> </td> <td> <center> Casa<br> <strong>2.00</strong> </center> </td> <td> <center> Casa<br> <strong>2.00</strong> </center> </td> </tr> </tbody> </table> 

If you have

<td id="cell1"></td> 

then in jQuery

$(function() { //this wrapper just tells the script not to run until the whole page is rendered
  $("#cell1").click(function(event) {  //this bit handles any user clicks on cell1
    console.log("clicked cell 1"); 
  });
}); 

will log to the browser console whenever the user clicks it.

If you want to run the same event for multiple cells then give them all a class and select against that instead (eg <td class="myGroup"> and $(".myGroup").click(... ).

This all uses standard jQuery syntax for binding to browser events - might be worth reading up on the basics of this.

This answer assumes you're looking to get values from the table by clicking on table cells.

For jQuery I would use a delegated approach to adding a click handler to all cells of the table. By using a delegated approach you are attaching fewer click handlers to the page and it will also work if you dynamically add cells to the table at a later time (ie Ajax).

Click on table cells in the Code Snippet to see their value copied to the green box.

 var $display = $('.display'); $('.my-table').on('click', 'td', function(e) { $display.text(this.innerHTML); }); 
 .display { width: 200px; margin: 1rem 0; padding: 0.5rem 1rem; border: 1px solid green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="my-table"> <tr> <th>First Name</th> <th>Last Name</th> <th>Age</th> </tr> <tr> <td>Aaron</td> <td>Anderson</td> <td>1</td> </tr> <tr> <td>Beth</td> <td>Black</td> <td>2</td> </tr> </table> <div class="display"></div> 

Note: As others have noted, the <center> tag is depreciated . Use text-align instead.

$("#cell1").click(function(){
$(this).css("background-color", "#ccc");   
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM