简体   繁体   中英

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

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.

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
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM