还记得 jQuery 弹出模式的关闭状态吗,localStorage 是最简单的方法吗?如果是,如何?

Remember close state of a jQuery popup modal, is localStorage the easiest way to do? If yes, how?

我正在尝试使用 localStorage 来记住用户单击“留在国际”时的选择。这样,弹出窗口就不会在用户访问页面时一直提示。

我找不到记住 jQuery 模式的关闭状态并将其放入 localStorage cookie 的方法。非常感谢您的帮助!

<!-- jQuery Modal -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />

<div id="countryModal" NAME="country" class="modal">
<a rel="modal:close" href="#" onclick="gotoInternational()">Stay at International</a>
</div>

<script>
setTimeout(function(){
  jQuery('#countryModal').fadeIn('slow').modal('show');
},3000)

function gotoInternational(){
//localStorage codes here
}
</script>

问得好! 如果我是你,我会多研究一下 localStorage。您完全正确地将值存储在 localStorage 中是最好的方法。 如果我理解正确你想做什么你的代码应该是这样的:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- jQuery Modal -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />

<div id="countryModal" NAME="country" class="modal">
    <a rel="modal:close" href="#" onclick="gotoInternational()">Stay at International</a>
</div>

<script>
    //Get current localStorage value
    const gotoInternationalValue = localStorage.getItem('gotoInternational');
    //Check if it is not true (can be false or null)
    if (gotoInternationalValue != 'true') {
        setTimeout(function() {
            jQuery('#countryModal').fadeIn('slow').modal('show');
        }, 3000)
    } else {
        console.log("Stay at international was already selected");
    }


    function gotoInternational() {
        //Save localStorage item as true
        localStorage.setItem('gotoInternational', true);
    }
</script>

如果这不是您需要的,请在您的问题中添加更多信息以进行澄清!

我找到了解决方案!

你的解决方案是正确的,只是你在这里漏掉了一个单引号:
if (gotoInternationalValue != 'true')

无论如何感谢您的意见!拯救了我!