0

I am working on a form and when you focus on an input field, I have a pop up that reminds them to save when they are complete. I only want the user to see this pop up once every 60 days. On focus I set a cookie that tells me they have seen the alert. Then however, since I only want this to appear every 60 days, I want to make sure it doesn't happen again next time they click. How do I execute something like, "if this cookie exists, then do this?" Below is a sample of the code I'm using:

$("input").focus(function(){
//My alert goes here (i'm good on this part")
Cookies.set('saveChanges', 'wasAlerted', { expires: 60, path: '/' });
//if SOMETHING, do SOMETHING ("need help here")
});

FYI, My cookie name is 'saveChanges' and the value is 'wasAlerted'.

https://github.com/js-cookie/js-cookie

4

1 回答 1

0

看到 cookie 的值是多余的,你可以检查是否有 cookie 匹配saveChanges

$("input").focus(function(){
    if (Cookies.get('saveChanges') == null) {
        //Alert
        Cookies.set('saveChanges', 'wasAlerted', { expires: 60, path: '/' });
    }
}

旁注,出于组织考虑,我建议使用jQuery Cookie 插件。API 并没有太大的不同,但它会让你的代码更加一致。

于 2015-12-10T19:05:55.297 回答