Ankit Chhatbar Ankit Chhatbar - 7 months ago 33
HTML Question

Getting value of <td> according to checked checkbox

I need to get value of according to checked checkbox... using HTML and jQuery or Javascript I have tried following code:

jQuery code:

<script type="text/javascript">
$(function () {
$('input:not(#submit)').click(function () {
t = $(this).attr('id');

text = $('.time' + t).text();
//alert(text);
});

$('#submit').click(function (e) {
e.preventDefault();
var trs = $('table tr');
var values = trs.first().find('td');

var values1 = $('table tr td :checkbox:checked').map(function () {
return $(this).closest('tr').find('td').text() + "==>"
+ values.eq($(this).parent().index()).text();
}).get();

alert(values1);

});
});
</script>


HTML markup:

<body>
<table border="1">
<tr>
<td>No</td>
<td >Name</td>
<td >City</td>
<td >Check</td>

</tr>
<tr>
<td>1</td>
<td>
select</td>
<td>
select</td>
<td>
<input type="checkbox" name="checkbox" class="r1" />select</td>
</tr>
<tr>
<td>2</td>
<td>
select</td>
<td>
select</td>
<td>
<input type="checkbox" name="checkbox" class="r1" />select</td>

</tr>
<tr>
<td>3</td>
<td>
select</td>
<td>
select</td>
<td>
<input type="checkbox" name="checkbox" class="r1" />select</td>

</tr>
<tr>
<td>4</td>
<td>
select</td>
<td>
select</td>
<td>
<input type="checkbox" name="checkbox" class="r1" />select</td>

</tr>
</table>
<input type="submit" name="submit" id="submit" value="Save time sheet" />

</body>


I need to get value of
<td>
of any row which checkbox is checked and I require multiple values also if multiple checkboxes are checked...

Answer

This is the code which giving me my desired output... I am posting it here for others help:

 <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>

    <script type="text/javascript">
    $(function () {
        $('input:not(#submit)').click(function () {
            t = $(this).attr('class');


            text = $('.time' + t).text();
            //alert(text);
        });


        $('#submit').click(function (e) {
            e.preventDefault();
            var trs = $('table tr');
            var values = trs.first().find('td');

            var values1 = $('table tr td :checkbox:checked').map(function () {
                return $(this).closest('tr').find('td:first').text()

            }).get();

            alert(values1);

            jQuery.ajax({
                type: 'post',                    
                url:'ddd.php',            
                data:{"values1" : values1},
                dataType:'json',    
                success:success            
            });  

        });

    });
    </script>