Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table which displays all users and the id for each user is stored in hidden field, i want to be able to get the value stored in the hidden on button click event using Ajax or Jquery.

What I have tried:

foreach (var del in approvedList)
 {         
   <tr>
    <td>@del.CampaignName</td>
    <td>@del.Name</td>                           
    <td>@del.ApprovalStatus</td>                            
    <td>  
    <div id="hiddenWrapper" class="row">
      <div class="col-sm-5"><input type="hidden" id="id" name="id" value="@del.PayoutId" />
          <input id="btnApprove" name="btnApprove" class="btn  btn-success" type="button" title="Approve" value="Approve" onclick="location.href='@Url.Action("PayoutApproval", "Account", new { id = @del.PayoutId })'" />
      </div>                                                                
    </div>                                                                                        
  </td>
 </tr>
 }


And the query function is :

 $('#btnApprove').click(function () {
    $("#btnApprove").prop("disabled", true);
    console.log("id field value: "+$("#id").val());
    $.ajax({
        url: '/Account/PayoutApproval',
        type: "POST",
        data: JSON.stringify({ id: $("#id").val() }),
        dataType: "json",
        contentType: 'application/json, charset=utf-8',

        success: function (result) {

            if (result.f != null) {
                swal(result.f, "!", "error");
                $("#btnApprove").prop("disabled", false);

            } else {
                swal({
                    title: "Success!",
                    text: result.s,
                    type: "success"
                });
                $('#action').prop('disabled', true).trigger("chosen:updated");
                $("#submitBrdFrm").prop("disabled", true);
                top.location.href = workListUrl;
            }
        }

    });

    return false;
});




While logging the hidden field value, i realized i could only get the value for first row only, subsequent rows return null. I want to be able to get value for each row upon button click, this is my challenge, i am new to front end development.
Posted
Updated 30-Jul-18 1:06am

1 solution

IDs in an HTML document need to be unique. You are assigning the same ID to the hidden input in each row. You are also assigning the same ID to each button.

There's also no point adding an onclick attribute to the button if you're simply going to cancel that handler elsewhere.

Change your markup code to:
HTML
<input type="hidden" name="id" value="@del.PayoutId" />
<input name="btnApprove" class="btn btn-success" type="button" title="Approve" value="Approve" />

Change the script to:
JavaScript
$(document).on("click", "input[name='btnApprove']", function(e){
    e.preventDefault();
    
    var btn = $(this);
    btn.prop("disabled", true);
    
    var id = btn.closest("tr").find("input[name='id']").val();
    console.log("id field value:", id);
    
    $.ajax({
        url: '/Account/PayoutApproval',
        type: "POST",
        data: JSON.stringify({ id: id }),
        dataType: "json",
        contentType: 'application/json, charset=utf-8',

        success: function (result) {
            if (result.f != null) {
                swal(result.f, "!", "error");
                btn.prop("disabled", false);
            } else {
                swal({
                    title: "Success!",
                    text: result.s,
                    type: "success"
                });
                
                $('#action').prop('disabled', true).trigger("chosen:updated");
                $("#submitBrdFrm").prop("disabled", true);
                top.location.href = workListUrl;
            }
        }
    });
});
 
Share this answer
 
Comments
Uwakpeter 25-Jul-18 9:56am    
Thanks, i really appreciate

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900