【问题标题】:Count from 1 to 10 and repeat inside a button link dont work从 1 数到 10 并在按钮链接内重复不起作用
【发布时间】:2017-06-25 18:11:16
【问题描述】:

我正在寻找一种方法来编写一个从 1 计数到 10 并且在 10 之后它会重复的脚本,在按钮链接内。

例子:

提交

第一次点击转到“url/page1.html”

第二次点击“url/page2.html”

三到“url/page3.html”

第四至“url/page4.html”

最多十次点击“url/page10.html”

在命中数 10 之后,它必须再次运行重复到 1。

示例2:

首先点击进入“url/page1.html”,点击2=“url/page2.html”,点击3=“url/page3.html”,点击4=“url/page4.html”,点击5= "url/page5.html, 点击5 = "url/page5.html, 点击6 = "url/page6.html, 点击7 = "url/page7.html, 点击8 = "url/page8.html, 点击9 = "url/page9.html click 10 = "url/page10.html 现在重复 click 11 = "url/page1.html, click 12 = "url/page2.html, click 13 = "url/page3.html, click 14 = "url/page4.html

现在我正在使用这个脚本,但它不重复计数器。 我想将用户发送到不同的链接,所以无论 ip 用户来自何处,如果它计数正确,ip 1 会在下一个访问者访问我的网站时转到 page1.html,然后单击按钮将转到 page2.html nxt 到 page3.html

立即编码:

 <button onclick="myScript(10)">Submit</button>

 <script>

 let currentPage = 1;

  function myScript(totalPages) {
  if (currentPage === totalPages) {
    return window.location.href=`/page10.html`
  }
   currentPage = currentPage < totalPages ? currentPage + 1 : 1;
   return window.location.href=`/page${currentPage}.html`;
  }

  </script>

希望有人能给我一个很好的例子来说明它应该如何运行,更喜欢 php 或 javascript。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    我认为你应该把你的代码改成这样:

    <button onclick="myScript(10)">Submit</button>
    
    <script>
    
    let currentPage = 1;
    
    function myScript(totalPages) {
        var location = '';
        if (currentPage % totalPages == 0) {
            location = '\/page10.html';
        }else{
            location = '\/page'+currentPage % totalPages+'.html';
        }
        currentPage++;
        return window.location.href = location;
    }
    </script>
    

    【讨论】:

    • 但是每次有新用户访问我的页面时不会从1开始?我需要一个脚本来记住最后一个访问者的哪一侧进入并进入下一页。
    • 我认为您应该使用 cookie 或类似的东西来存储最后访问的页面。 Javascript 自己无法做到这一点。
    【解决方案2】:

    试试这个:

    <button onclick="myScript()">submit</button>
    var i=1;
    function myScript(){
       i+=1;
       if (i==1){
         window.location.href=`/page${i}.html`;
         i++;
         return;
       }
       if (i==10){
         i=1; 
       }
       window.location.href=`/page${i}.html`
    }
    

    【讨论】:

    • 但是每次有新用户访问我的页面时不会从1开始?我需要一个脚本来记住最后一个访问者的哪一侧进入并进入下一页。
    • 是的,如果你在我的电脑上按 4 次它会正常工作,但如果我尝试在我的手机上按第五次它会再次从 1 开始。它不必继续,因为在同一设备上点击了 5 次。希望你明白:)
    • 您应该尝试将变量 'i' 保存在某个地方,也许在 php 中,抱歉,我不知道这个啊
    【解决方案3】:

    试试:

    <button onclick="myScript(10)">Submit</button>
    
    <script>
     //get current url
     let url = new URL(window.location.href);
    
     //get currentPage
     let currentPage = parseInt(url.searchParams.get("currentPage")) || 0;
    
     function myScript(totalPages) {
    
       currentPage = currentPage < totalPages ? currentPage + 1 : 1;
    
       return window.location.href=`/page${currentPage}.html?currentPage=${currentPage}`;
     }
    
    </script>
    

    从 0 开始,您可以摆脱第一个 if 语句。当用户第 11 次点击按钮时 currentPage (10) 不小于 totalPages (10),因此您的代码会将 currentPage 重置为 1。

    【讨论】:

    • 但是每次有新用户访问我的页面时不会从1开始?我需要一个脚本来记住最后一个访问者的哪一侧进入并进入下一页。
    • 我更新了我的答案。更新尝试将 currentPage 存储为 get 参数,这应该使代码知道用户所在的当前页面。
    • 我这边什么都没发生nltrading.dk/routingscript/page.html
    • 更新帖子:我有错误的空合并运算符。它现在应该做点什么。
    • 它现在可以工作了,但是当我来自一个新的 ip 时,它必须从头开始,它必须继续
    猜你喜欢
    • 2017-11-27
    • 2023-04-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2018-11-19
    相关资源
    最近更新 更多