简体   繁体   中英

How i can get value of specific cell if row is checked?

I have an HTML table. I need to get specific cell value if column is checked! For each row I have a checkBox.

<table id="tabellaOrdinaFarmaci" class="table table-striped table-bordered" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Codice Farmaco</th>
                    <th>Nome Farmaco</th>
                    <th>Quantità </th>
                    <th>Quantità di alert</th>
                    <th>Stato</th>
                    <th>Quantità da ordinare</th>
                    <th></th>
                </tr>
            </thead>
        </thead>
        <tbody>
            <?php
            foreach ($query as $row):
                $quantitaDaOrdinare = $row->alert - $row->quantita;
                if ($quantitaDaOrdinare > 0) {
                    ?>
                    <tr id="<?php echo $row->aic; ?>" > 
                        <td ><?php echo $row->aic; ?></td>
                        <td name="nomeFarmac"o><?php echo $row->denominazione ?></td>
                        <td><?php echo $row->quantita; ?></td>
                        <td><?php echo $row->alert; ?></td>
                        <td><?php echo $row->stato; ?></td>
                        <td>
                            <input type="text" class="form-control auto" name="codiceFarmaco" value="<?php echo $quantitaDaOrdinare ?>"/>
                        </td>
                        <td>   <label>
                                <input type="checkbox" name="checkBox" value="<?php echo$row->aic; ?> ">
                            </label></td>
                    </tr>
                    <?php
                } else {

                }
            endforeach;
            ?>
        </tbody>

I need (using javascript or jquery) to get, if a button is clicked, every value of cell that have name="nomeFarmaco" for every row that is checked! I try to call this function whe

function getValue(){
     var nome = $('input[name="checkBox"]:checked').map(function() {
                            return $(this).parent().parent().parent().find('name="nomeFarmaco').val();
                        }).get();
return nome;}

but nome is an empty array! any ideas? thanks in advance!

I think this is typo?

<td name="nomeFarmac"o><?php echo $row->denominazione ?></td>

Change it to:

<td name="nomeFarmaco"><?php echo $row->denominazione ?></td>

And you are targetting a <td> and .val() won't work. Should be .text()

var name = [];
$('input[name="checkBox"]').click(function(){
    if($(this).is(':checked')) {
        name.push($(this).parent().parent().siblings('td[name="nomeFarmaco"]').text());
    } else {
        var index = name.indexOf(name);
        name.splice(index, 1);
    }
    console.log(name);
});

You can put a custom attribute for the check box and set the value in this attribute. Like this :

<input type="checkbox" name="checkBox" value="<?php echo$row->aic; ?> " customattr="<?php echo $row->denominazione ?>">

And you javascript function will be :

function getValue(){
    var nome = $('input[name="checkBox"]:checked').attr('customattr');
    return nome;
  }

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