-1

I have this code whereby in a table row there are checkboxes and input boxes, I would like to get the values of all input boxes whose checkboxes are checked

This is the html

<table>
<tr>
<td><input type="checkbox" name="new" value="37"></td>
<td><input type="text" name="new" id="quantity" class="37"></td>
</tr>

 <tr>
 <td><input type="checkbox" name="new" value="38"></td>
 <td><input type="text" name="new" id="quantity" class="38"></td>
 </tr>
  .....#this continue
   </table>

Note that the class of the input boxes is the same as the value of checkboxes

Currently am using this code to check all the checked checkboxes

    var marked = new Array();
    var k = 0;
    $('input:checked').each(function(index,value) {

        marked[k] = value;
        k++;
        alert(k);
    });        
    alert($('input[name="new"]:checked').serialize());

How can i change it to return the value of the input boxes

Geoff
  • 6,277
  • 23
  • 87
  • 197

2 Answers2

1

Try this:

$(document).ready(function(){
        $( "input[type=checkbox]" ).each(function(){
            if($(this).is(':checked'))
            {
                var value = $(this).closest('tr').find($( "input[type=text]" )).val();
                alert(value);
            }
        });
    });
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
0

var arr=[];

$(':checkbox:checked').each(function(){

arr.push($(this).parent().next().find('input').val())
//arr.push($(this).val())

})

console.log(arr)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
  <td>
    <input type="checkbox" name="new" value="37" checked>
  </td>
  <td>
    <input type="text" name="new" id="quantity" class="37" value='asd'>
  </td>
</tr>

<tr>
  <td>
    <input type="checkbox" name="new" value="38" checked>
  </td>
  <td>
    <input type="text" name="new" id="quantity" class="38" value='qwe'>
  </td>
</tr>
</table>

Try this way

guradio
  • 15,524
  • 4
  • 36
  • 57