简体   繁体   中英

How to store value and status of checkbox at pageload

I wish to store the values of checkboxes along with their status at the time of page load. When I submit the form I want to check if the checkbox 'checked' earlier is 'unchecked'; if so I need to save the values in a string to be used as list in the query. I have tried the following but something is wrong with my syntax. Please help.

<input type="checkbox" name="Periods" value="25301601" />
<input type="checkbox" name="Periods" value="25301602"  checked />

<input type="button" id="buttonStore" value="Store">
<input type="button" id="buttonCompare" value="Compare">
$(document).ready(function() {
    $("#buttonStore").click(function () {
        // save status and value of all the checkboxes
        var periodCHK= [];
        $(":checkbox").each(function (){                            
            periodCHK["$(this).val()"]=($(this).is(':checked'));//does not store
             alert($(this).is(':checked'));
            alert($(this).val());
        });
        alert(periodCHK);//nothing
        }); 

     $("#buttonCompare").click(function () {     
        var periodstring; 
        $(":checkbox").each(function () {
            if (!$(this).is(':checked'))
                //if $(":checkbox").not(':checked')
            {
                //if this one was checked before, grab it and make a string
                if (periodCHK["$(this).val()"]) 
                {
                    periodstring+=["$(this).val()"] + ",";
                }
            }
            alert(periodstring); // does not loop
        });
    });
});

Use $(this).val() without quotes in the following statement. if used with quotes it will be treated as string.

periodCHK[$(this).val()]

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