【问题标题】:Tick checkbox if page is visited via button如果通过按钮访问页面,请勾选复选框
【发布时间】:2021-09-24 08:49:29
【问题描述】:

寻求一些帮助以了解如何添加以下功能:

我的 WP 网站上有一个联系页面,其中有一个联系 7 表格和一个用于注册时事通讯的复选框。

可以通过两种方式访问​​联系页面 - 显然是通过菜单,但主页上还有一个“为我注册时事通讯”按钮,当点击该按钮时,当前也会将用户带到联系页面。我想这样当用户点击主页上的按钮时,它不仅会将他/她带到联系页面,而且还会自动勾选那里的复选框。如果用户通过菜单或任何其他方式访问联系页面,我不希望自动勾选复选框。

我想我正在寻找一些 php 逻辑,但任何想法都值得赞赏。

【问题讨论】:

    标签: php wordpress checkbox


    【解决方案1】:

    将 PHP GET 字符串添加到按钮链接,然后在另一侧读取该字符串并制作如下逻辑:

    如果页面是contact-page.php,则在按钮上添加链接contact-page.php?button=true

    contact-page.php 上使用 PHP 阅读 ?button=true

    if (isset($_GET["button"])) {
       //echo that checkbox checked 
    }else{
       //echo that checkbox not checked 
    }
    

    或在 JS 中:

    if (window.location.href.indexOf("button") > -1) {
          //check checkbox with JS
      }
    

    其他选项是您使用 JS localStorage ,当您单击设置为 localStorage button: true 的按钮时,然后在 contact-page.php 上阅读它,如果它为真,请使用 JS 检查复选框

    【讨论】:

    • 处理搜索参数时有更好的方法:const SP = new URLSearchParams(location.search); console.log(SP.get("button")); // "some value" otherwise null 阅读更多:MDN URLSearchParams(如果您不关心 IE)
    猜你喜欢
    • 2019-05-23
    • 2020-09-14
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 2013-05-28
    • 1970-01-01
    • 2016-04-26
    相关资源
    最近更新 更多