lundi 29 juin 2015

Appending URL using jQuery based on whether a checkbox is checked

I am trying to append a URL using jQuery.

I have a grid of 'warnings' on my page, populated from a servlet. There is a check box at the top of the screen, which when checked will show the cleared warnings along with the active ones. Basically, this check box is showing and hiding rows of the grid. There's a button to clear a warning, and a button to unclear warnings. The unclear button shows when the checkbox is checked.

Upon first click of the check box, the URL is appended correctly. However, when unchecking the check box, the unclear button sticks and the URL is not properly appended.

Here's my jQuery:

 $(document).ready(function() 
   {
      var showClearedWarningsFlag = getURLParameter("showHideClearedWarnings");

  if(showClearedWarningsFlag = "SHOW")
  {
     $('#showClearedWarningsCheckBox').prop('checked', true);
     $('#unclearButton').show();
  }
  else
  {
     $('#showClearedWarningsCheckBox').prop('checked', false);
     $('#unclearButton').hide();
  }

});


 $('#showClearedWarningsCheckBox').change(function ()
 {
    if($(this).is(':checked'))
    { 
       window.location.search = "?showHideClearedWarnings=" + "SHOW";    
    }
    else
    {
       window.location.search = "?showHideClearedWarnings=" + "HIDE";
    }

 });

$('#clearButton').click(function(){
         $('#bagWarningAction').val('CLEAR') ;
    });

   $('#unclearButton').click(function(){
       $('#bagWarningAction').val('UNCLEAR') ;
   });

So, what I am trying to do here:

if URL is show
   then show unclear button and check box
else if URL is hide
   don't show unclear button and don't check box
on change of checkbox
   if checked
      append URL to show
   else if not checked
      append URL to hide

Then, I have the two buttons for clear and unclear passing through SHOW or HIDE to a hidden input on my page.

However, what is happening is when I press the check box for show cleared warnings, this is successfully appending the URL, but leaves the checkbox unchecked on the page refresh. So, upon checking the checkbox again, it's just redirecting to the SHOW URL. If I manually change the URL to HIDE, it hides the cleared warnings.

I think I am missing something very subtle here. If anyone could shed light as to why the checkbox isn't surviving the refresh, I would be very grateful!




Aucun commentaire:

Enregistrer un commentaire