douweida2878 2014-09-28 19:56
浏览 30
已采纳

根据页面上的网址编辑复选框

I have been struggling for a few hours with something that sounds very simple. What i have is a list of checkboxes, if a checkbox is checked the page url will change (to save a filter option, so you can share the url to the filter). It works perfectly except one point, when the user wants to go back. the checkboxes should be unchecked.

Using jQuery

setBoxes() is the function;

I tried:

function pageLoad() {
   setBoxes();
}

And

$(document).ready(function(){
   setBoxes();
}

Also I added

 window.onunload = function(){};

But none of these solutions worked ( the function doesn't run, i checked it).

After that I thought I might could do it with php (get the url to an array, if in the array echo checked), but php wont run again (I guess its cashed).

Is there any option i can try cause I really cant solve this simple looking problem.

because my code is very big I made a very small example http://jsfiddle.net/yzoztp05/1/ of the situation, updating the url in jsfiddle is blocked so it doesn't work completely.

To make the situation more clear i made a video https://www.youtube.com/watch?v=jaSc1fmaJD4&feature=youtu.be.

the code to set my checkboxes

//convert the url to array
function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

// Set the checkboxes to checked if they are in the array
function setBoxes()
{
    var prijsUrl = getQueryVariable("prijs");
    // first set all checkboxes on unchecked
    $('.prijsFilterBox').prop('checked', false);
    alert("Loaded");
    if(prijsUrl != false)
    {           
        var res = prijsUrl.split("-");

        for(var i = 0; i < res.length; i++ )
        {
            $('.prijsFilterBox[value='+res[i]+']').prop('checked', true);
            console.log("prijs = "+res[i]);
        }
    }
    else
    {
        console.log("prijs = null");
    }
}

It works if I refresh the page, but it wont run on when user presses the back button. If its not possible to run javascript on page back I will try to make a different script with php.

TLDR How can i run a jquery function on page back (all browsers)?

  • 写回答

1条回答 默认 最新

  • douya1248 2014-09-29 00:50
    关注

    It may be possible ( and I think that many are using this way ) to have timeOut function that is called every 500ms that checks if URL has changed, but instead of using '?' use hash '#' so user is always on same page and you can catch URL change.

    If you need to stick to '?' , maybe you could do something with window.onbeforeunload function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求一位精通京东相关开发的专家
  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多