Homer_J
Homer_J

Reputation: 3323

jQuery checkboxes checked when posting data

I have a series of checkboxes on a page and when the page is loaded for the first time I would like them all checked by default.

However, once a user de-selects certain boxes and then submits the page (it posts back to the same page) I'd like their selections to be shown and not all checkboxes checked by default.

Currently I have the following:

$( document ).ready(function() {
    $('.sdate').attr('checked', true);          
});

This checks the checkboxes but I can't work out the logic to then only show then users selection when the post back happens.

Upvotes: 0

Views: 105

Answers (1)

Adil Abbasi
Adil Abbasi

Reputation: 3291

If you are using any server side language like php or any other you can try like this, php example.

<script type="text/Javascript">

   $( document ).ready(function() {
      var was_posted = <?php echo ('POST' === $_SERVER['REQUEST_METHOD']) ? 'true' : 'false'; ?>
      if(was_posted != true){
         $('.sdate').attr('checked', true);  
      }
   });
</script>

Upvotes: 1

Related Questions