Jquery calculation values per row

MiguelBT 96 Reputation points
2022-02-14T15:43:42.58+00:00

Hi, I don't know if it's correct but how is it an MVC project.

I have a jquery that performs an account when entering a value.

$(document).on("change keyup blur", "#startValue", function () {  
            var main = @ViewBag.sumMin;  
            var disc = $('#startValue').val();  
            var mult = (main * disc).toFixed(2);  
            $('#result').val(mult);  
        });  

the previous code working
174446-calc.gif

my structure
174420-image.png

I also intended to do this account with a value in each row of a table
i try this:

UPDATE (for some reason I can't insert the code in format (permission error any help?)

174419-image.png

I tried closing the rows but without success, does it work through a cycle going through the IDs.
Thanks for all help.

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sreeju Nair 12,756 Reputation points
    2022-02-14T16:09:54.457+00:00

    if you do the keyup event on document, the "this" object refer to the document. so if you want this to be for each table row or cell, do the event on corresponding element.

    $(".your table row class").on("change keyup blur", "#startValue", function () {
    var disc = $('#startValue').val();
    var numpec = $(this).closest('tr').find("#numpec").val();
    var multi = (numpec * disc).toFixed(2);
    $(this).closest('tr').find("#resultTR").val(multi);
    });

    0 comments No comments

  2. Lan Huang-MSFT 30,206 Reputation points Microsoft External Staff
    2022-02-15T07:44:28.573+00:00

    Hi @MiguelBT ,
    I think it should be the problem written in $(this).closest('tr').find, maybe you can provide more detailed code.

    <td >  
      <input type="text"" />  
    </td>  
    

    Replace

    $(this).closest('tr').find("#resultTR").val(multi);  
    

    Should be

     $(this).closest('tr').find('input[type="text"]').val(multi);  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.