jQuery selector updated values get reset when posting form - how to prevent?
I have an HTML form consisting of checkboxes, select boxes, etc., each with a numeric value for which I'm using jQuery selectors to add up the total, and display the number in a separate div. Here's one:
<label for="OptionalExtra">
<p><input name="OptionalExtra" type="checkbox" class="calc" value="200" /> Add £200</p>
</label>
Then I use the :checked selector to loop through them all, adding the total up.
$('label').change(function() {
var total = 0;
$('开发者_JS百科.calc:checked').each(function() {
total += parseInt($(this).val());
});
});
I can then display this total elsewhere.
When I submit the form, however, the div holding my calculated value remains there (I do want this), but the value resets (I don't want this - I need it to show the previously calculated value).
Of course, I know why that happens, but I don't know how to stop it!
Please help.
So why does it happen then if you know why?
Is it a full submit with input type=submit, or an ajax submit with $.post()?
It sounds like a full submit - which means that your entire page is resent from the server and your calculated div will be put back to it's original value.
精彩评论