0

I want to get the values of selected check box row's value using Jquery.

For that, First I try to alert a message,if user checked the checkbox using Jquery. But I failed.

Note: I am adding checkbox in a row using Jquery, and then User will select the checkbox.

Here is my code to add checkbox in row through AJAX code

$('#btnSearchVendor').click(function(){
    $.ajax({
        type:"POST",
        contentType:"application/json;charset=utf-8",
        url: "GPCreateCheque.aspx/BindData",
        data:"{}",
        dataType:"json",
        success: function (data) {
            alert("success = "+data.d[0].DOCTYPE);
            for(var i=0;i<data.d.length;i++){
                $("#ContentPlaceHolder1_GridView1").append("<tr><td><input id=\"Checkbox"+i+"\" class=\"checkBoxClass\" type=\"checkbox\" /></td><td>" + data.d[i].DOCTYPE + "</td><td>" + data.d[i].DOCNUMBR + "</td><td>" + data.d[i].ActualAmount + "</td></tr>");
            }
        },
        error:function(result){
            alert("Error");
        }
    })
})

Jquery code to make alert when user select the checkbox

$('.checkBoxClass').click(function () {
    alert("Checkbox state (method 1) = " + $('.checkBoxClass').prop('checked'));
    //alert("Checkbox state (method 2) = " + $('.checkBoxClass').is(':checked'));
});

Above code is working fine, I added the checkbox. Below pic is the output with console Screenshot of output

Liam neesan
  • 2,282
  • 6
  • 33
  • 72

2 Answers2

1

You can use following script for this

$(document).on('click','.checkBoxClass',function () {
    alert("Checkbox state (method 1) = " +  $(this).prop('checked'));

    if($(this).prop('checked') == true) {
        alert("Actual Amount = " + $(this).closest('tr').find('td:last-child').html());
    }
});
Super User
  • 9,448
  • 3
  • 31
  • 47
1

You can use this code to get the corresponding row value of the clicked checkbox

$(document).ready(function() {

  $('.checkBoxClass').on('click', function() {
    debugger
    if ($(this).prop('checked')) {
      var columns = $(this).closest('tr').find('td:not(:first-child)');
      $(columns).each(function(index) {
        console.log("Column " + (index + 1) + " has value " + $(this).text());
      });
    }

  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border='1'>
  <thead>
    <tr>
      <th></th>
      <th>Head 2</th>
      <th>Head 3</th>
      <th>Head 4</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input type='checkbox' id='checkbox1' class='checkBoxClass' /></td>
      <td>Content 2</td>
      <td>Content 3</td>
      <td>Content 4</td>
    </tr>
    <tr>
      <td><input type='checkbox' id='checkbox1' class='checkBoxClass' /></td>
      <td>Content 222</td>
      <td>Content 3333</td>
      <td>Content 4444</td>
    </tr>
  </tbody>
</table>

It makes sure that value is printed on console only when checkbox is selected.

Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62