问题
I am trying to reset a cookie's val to "1" every time in the ready function, however it seems as if my ready function is not getting reached at all.
First, I got the following code to get and set cookies without using a plugin here.
function setCookie(key, value) {
var expires = new Date();
expires.setTime(expires.getTime() + 31536000000); //1 year
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}
function getCookie(key) {
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
return keyValue ? keyValue[2] : null;
}
Here's how I try to set the cookie to "1" in the ready function:
$(document).ready(function () {
var rowcount = new Number(1);
setCookie('dynamictablerows', rowcount);
alert('ready function reached');
});
Then, in the load function, I have a temporary "debug" alert to show the current value in the cookie:
/* NOTE: Things that need to take place after all the elements have been constructed need to go here; the ready function can be too early */
$(window).load(function () {
. . . non-pertinent-to-this-question code elided
alert('load function reached ' + getCookie('dynamictablerows'));
});
I update the value of the cookie when the user selects a "add a row" button:
/* This makes the next hidden row visible, as long as there is one */
$(document).on("click", '[id$=btnAddFoapalRow]', function (e) {
$('[id$=foapalhtmltable]').find('tr:hidden:first').show();
var currentvisiblerows = new Number(getCookie('dynamictablerows'));
currentvisiblerows = currentvisiblerows + 1;
setCookie('dynamictablerows', currentvisiblerows);
});
So, I should see "1" with the first alert, and then "3" after adding two rows and selecting the "Save" button (which refreshes and again calls the ready and load functions).
What I do see, though, is no 'ready function reached' alert at all; and then, after adding two rows by clicking the "btnAddFoapalRow" button, and thereafter submitting the page, the alert always simply has the previous value displayed plus and additional 2; for instance (no pun intended), if it starts off with telling me "load function reached 13", it then says "load function reached 15".
So the cookie is being incremented correctly, but never gets reset to 1, as the ready function is not being reached. Shouldn't the ready function be called before the load function?
My hope was that the ready function would only fire once per site visit, and so I could [re]set the cookie val to 1 then, and then the load() function would run every time the form was submitted (N times per site visit), thus knowing how many rows would be needed to add back to the table. IOW, the table always starts with two rows; the user can add a third and then a fourth row via btnAddFoapalRow. The problem arises in that the dynamically added rows are removed with each form submit, and so I want to programmatically add them back following form submission, and the way to know how many to add back is (theoretically) by querying the cookie val.
So what am I doing wrong or misunderstanding here?
UPDATE
Now for some reason the ready function is being reached (I am seeing the alert), although I made no changes to the jQuery. Maybe it's being affected by the phase of the moon or has been subjected to some internal smelling salts of some sort.
来源:https://stackoverflow.com/questions/31392190/why-is-my-ready-function-not-reached