2014-06-28 33 views
0

我有一個HTML表格。如果列已被​​選中,我需要獲取特定的單元格值!對於每一行我都有一個複選框。如果檢查行,我可以如何獲得特定單元格的值?

<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> 

我需要(使用JavaScript或jQuery的)來獲得,如果一個按鈕被點擊時,細胞的每個值有NAME =「nomeFarmaco」對於每個被檢查行!我嘗試稱這個功能爲:

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

但是nome是一個空數組!有任何想法嗎?提前致謝!

+0

你想獲得哪一個小區的文本? –

+0

有name =「nomeFarmaco」的單元格 –

回答

0

您可以爲該複選框設置自定義屬性並在此屬性中設置值。 像這樣:

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

你javascript函數爲:

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

複選框有另一個我使用的屬性:D –

+0

它工作嗎? –

+0

獲取複選框的屬性我使用var aicFarmaco = $('input [type = checkbox]:checked')。map(function(_,el){return el()).val(); })。得到();但現在我需要獲取特定單元格的文本! –

0

我認爲這是錯字?

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

將其更改爲:

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

而你靶向一個<td>.val()將無法​​正常工作。應該是.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); 
}); 
+0

同級不工作 –

+0

這樣數組就是空的! –

+0

@ ermanno.tedeschi你不能直接使用'.map()',你需要以數組形式創建值,然後使用'.map()'。 –

相關問題