0

i have couple textboxes on my form. Few expenses and a balance.

I use javascript to calculate the balance when the expense textboxes get changed like so:

$(document).ready(function () {


    // add up main numbers
    $("input[class*='add']").change(function (event) {
        var sum = 0;
        var subsum = 0;
        var num = 0;
        $("input[class*='add']").each(function (event) {
            num = parseInt($(this).val()) || 0;
            sum = sum + num;
        });
        $("input[class*='minus']").each(function (event) {
            num = parseInt($(this).val()) || 0;
            sum = sum - num;
        });
        $("input[class*='addcost']").each(function (event) {
            num = parseInt($(this).val()) || 0;
            subsum = subsum + num;
        });
        var balance = $("input[id*='txtBegBal']").val() - sum;
        $("input[id*='txtEnding']").val(balance);
        $("input[id*='txtTotalCost']").val(subsum);
    });

the results show correct on screen, however when I click on "save", the txtTotalCost still shows the value, before javascript has updated it, so it does not get saved correctly.

could someone please point me in the right direction?

I do all my populating within the if (!Page.IsPostBack). The values that I change for "expenses" get passed in just fine, it's just the calculated field that is not.

2
  • where are rest codes which is saving the changes. And what is happening on save click.. Commented Oct 3, 2013 at 16:05
  • Could you possibly put together a jsfiddle showing that it isn't working? Commented Oct 3, 2013 at 16:18

2 Answers 2

2

Just a guess here but is the txtTotalCost disabled? Disabled text fields will not update like this because the client browser doesn't send the form data back to the server when it is disabled. In that case, you'll get the old value that is in viewstate. Try enabling it, or you could use a label and a hidden input field.

Sign up to request clarification or add additional context in comments.

Comments

0

The second suggestion is that your fields haven't exist yet. Try to use

 $("input[class*='add']").on('change', function() { 
     // your code here 
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.