Home Ask Login Register

Developers Planet

Your answer is one click away!

Fahad Alt 2 weeks ago

Passing row value from input to ajax post

I have a table with multiple rows with different values - values are inside hidden input - Just like this:

<tr> 
   <td>UserName 1 <input type="hidden" class="EmployeeID" name="EmployeeID[]" value="1" />
   <td><input type="button" class="btn btnDelEmployee" value="Delete" /></td>
</tr>
<tr> 
   <td>UserName 12 <input type="hidden" class="EmployeeID" name="EmployeeID[]" value="2" />
   <td><input type="button" class="btn btnDelEmployee" value="Delete" /></td>
</tr>

I am trying to pass the value of the row once I click the delete input. I tried this code:

$(document).on('click', '#btnAddClose', function () {
      $.post('', {btnAddClose:1, EmployeeID: $(".EmployeeID").val()}, function(data){

      },'json');                    
 });

When I click the second row I get the value of the first row, instead of getting the value of the second row.

Answers


user1942990 2 weeks ago

I recommand you to add an attribute to your field like :

<input type="button" class="btn btnDelEmployee" value="Delete" data-id="2" />

Then :

$(document).on('click', '#btnAddClose', function () {
      $.post('', {btnAddClose:1, EmployeeID: $(this).attr('data-id')}, function(data){

      },'json');                    
 });


erwan 2 weeks ago

Well, you can try that tho

<tr> 
   <td>UserName 1 
   <td><input type="button" class="btn btnDelEmployee" value="Delete" data-employeeID='1' /></td>
</tr>
<tr> 
   <td>UserName 12 
   <td><input type="button" class="btn btnDelEmployee" value="Delete" data-employeeID='2' /></td>
</tr>

javascript

$(document).on('click', 'input[type=button].btnDelEmployee', function () {
      $.post('', {btnAddClose:1, EmployeeID: $(this).data('employeeID')}, function(data){

      },'json');                    
 });

Post Status

Asked in 2 weeks ago
Viewed 2,936 times
Voted 11
Answered 2 times

Search




Leave an answer


Quote of the day: live life