Update Stock Input Value for each Same Item Selected

Update Stock Input Value for each Same Item Selected

I have a problem, I need to update the stock value (real-time) for each select box that has the same Item, for example on the image attached, there are two select input that has the same item.




I want the stock quantity on the second row to be updated depending on the difference of stock quantity and quantity input.

Example:


1st Row: QTY: 3000 , STOCK : 6000
2nd Row: QTY: 5000 , STOCK : 3000 (3000 because on the 1st row the stock has been deducted by 3000)



Hope you can help. Here's my code below, Thank you

  1. $('select[name="selector[]"]').change(function () {
  2. $.ajax({
  3. url: 'changeitem',
  4. context: this,
  5. dataType: 'json',
  6. data: {
  7. 'itemid': this.value
  8. },
  9. success: function(info) {
  10. var soid = $('#soid').val();
  11. $(this).closest('td').next().next().find("#xstock").val(info.stock);
  12. $.ajax({
  13. url: 'getsoitemqty',
  14. context: this,
  15. dataType: 'json',
  16. data: {
  17. 'itemid': this.value,
  18. 'soid': soid
  19. },
  20. success: function(info) {
  21. $(this).closest('td').next().find("#xqty").val(info.qty);
  22. var stock = 0;
  23. var qty = 0;
  24. var needed = 0;
  25. stock = $(this).closest('td').next().next().find("#xstock").val();
  26. qty = $(this).closest('td').next().find("#xqty").val();
  27. needed = parseInt(qty) - parseInt(stock);
  28. if(needed<=0) {
  29. needed = 0;
  30. }
  31. $(this).closest('td').next().next().next().find("#xneedqty").val(needed);
  32. }
  33. });
  34. }
  35. });
  36. });