简体   繁体   中英

How to check a function before execute another one

The problem is that i have an function like this :

function toggleLayers(layers,id)
    {
        var i, l = layers.length;
        if ($('#'+id).is(':checked')) {
            for(i = 0; i < l; i++){
                layers[i].setMap(map);
            }
        }
        else
        {
            for(i = 0; i < l; i++){
                layers[i].setMap(null);
            }
        }
    }

With this feature I check whether it is a checked checkbox or not. The problem is with another function:

function refreshpage(){
        var sdat = document.getElementById('refreshi').value ;      
        var enddate13 = sdat.replace('.','');       
        var enddate1 = enddate13.replace('.','');       
        var enddate2 = enddate1.replace(':','');        
        var enddate3 = enddate2.replace(':','');        
    //    var endate = "*";     
        var date = enddate1.slice(0, 8);        
    var time = enddate3.slice(9, 15);       

        var endateD = date.slice(0, 2);     

        var endateM = date.slice(2, 4);     
        var endateY = date.slice(4, 8);     

    var endateFinal = endateY + endateM + endateD ;


}

This function refreshes the site and if the checkbox is checked, this checbox will be unchecked. how can i do to work even if the site refreshes if the checkbox is checked to be checked?

When a web page is refreshed any state you had in it, in terms of form data like checkboxes, is deleted. You have several options:

  1. Use local storage . Save the data in local storage and when the page is loaded read from the local storage and set the checkboxes.
  2. Use your server. Send the form data (which checkboxes are checked and so forth) to the server and when the page is loaded make the server send the data to the page. This is not the best solution since your'e saving page state in the server.
  3. Use the url. You can save the state of the form in the url using the query string. look here . This means that each checkbox toggle will change the url without reloading.

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