How to hide or display DIV based on checkbox state on different page?

♀尐吖头ヾ 提交于 2019-12-13 05:58:01

问题


I have an app in python django and there's summary page with 5 sections. Lets call them A, B, C, D, E. The first three (A, B, C) are generated automatically without doing anything. while last two (D, E) are generated based on check-box state on a completely different page.

I am trying to use jQuery to accomplish this so far i have tried this but it only works if those DIV's are on same page. I tried googling but couldn't find any solution.

$(".checkbox").click(function(e){
    var checked = $(this).is(':checked');
    if(checked==true){
        //display those content
    }
});

I can not even pass it as URL parameter because i have a button on same page as check-boxes which when clicked displays the summary page in pdf format.

FYI: I am using an hidden iframe on the pages with button (Well button is also on four different pages) which have content of summary page and is shown as PDF on button click.


回答1:


use a cookie easy and nice.

you can set a cookie like the below and then read it through the javascript

set cookie:

   document.cookie="checkbox=true";

read cookie on next page

   var value = readCookie('checkbox');

create a function which allows you to get the value back each time

 function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

so now when you compare it with the click you can do something like this:

$(".checkbox").click(function(e){
    var checked = $(this).is(':checked');
    if (checked == undefined || null){
        checked = readCookie('checkbox');
    }
    if(checked==true){
        //display those content
    }
});


来源:https://stackoverflow.com/questions/31479233/how-to-hide-or-display-div-based-on-checkbox-state-on-different-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!