gigebyte / cookies

Automatically exported from code.google.com/p/cookies
0 stars 0 forks source link

cookieFill doesn't work on a checkbox #34

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps reproduce the problem?
1.$("#ssl").cookify(). I used cookie to record whether user would like to use 
ssl. $("#ssl") will select a input element with type "checkbox" and id "ssl".

2.call $("#ssl").cookieFill() to set checkbox to user preference.

cookie can be set correctly, but cookieFill() can't change the status of the 
checkbox.

What is the expected result of the above steps?  What do you see instead?
checkbox will be checked or unchecked according to the value of cookie.

What version of the following are you using?
  jQuery:1.4.4
  cookies:2.2.0

What browser/version are you using?
Firefox 3.6.13

What OS/version are you using?
Windows7

Do you have any additional information to provide?
No

Original issue reported on code.google.com by exceed...@gmail.com on 9 Feb 2011 at 4:01

GoogleCodeExporter commented 9 years ago
I can verify a portion of this defect. I built a test page with a check box 
that has an ID of "ssl". If I check the box and call ;cookify() on it, I see 
the cookie get set.  I refresh the page and when it loads the box is not 
checked (expected). I run .cookieFill() on the check box and it gets checked 
(expected).

Up to that point all is well.

Then  delete the cookie via $.cookies.del( 'ssl' ). I verify it is gone and 
then call .cookieFill() ont ehc heck box again. The checkbox is not removed.

Does this match what you're seeing?

Thanks very much,
Jim

Original comment by auldrid...@gmail.com on 9 Feb 2011 at 4:34

GoogleCodeExporter commented 9 years ago
Almost it matches. I even couldn't get checkbox checked after page loaded then 
called .cookieFill(). 

I tested again, my checkbox has a default value "true". <input id="ssl" 
type="checkbox" value="true">

This is the reason. Everything goes well after I removed the default value.

But We need to set ssl checked as default value before we record user's 
preference.

Original comment by exceed...@gmail.com on 10 Feb 2011 at 1:13

GoogleCodeExporter commented 9 years ago
I was having this same issue.
Tracked it down to Line 342 in the "cookify" function.
I changed:
    if( $this.is( ':checkbox, :radio' ) )
    {
       if( $this.attr( 'checked' ) )
       {
           value = $this.val();
       }
    }

to:

if( $this.is( ':checkbox, :radio' ) )
{
    value = $this.attr( 'checked' ) ? 'on' : 'off';
}

and now the "cookify" function stores the actual value of the checked attribute.

Prior to this, the cookie value would be set to "on", or the cookie would be 
expired in .set because the value would be set to null.

By storing the value of the checked attribute even when it's off/false, 
cookieFill is now able to restore the value (otherwise the default value takes 
precedence)

I hope this helps someone.

Finally, kudos to James Auldridge for doing the legwork on this library.
I love the entire concept of a jQ cookieBind.

Pete

Original comment by pete....@gmail.com on 10 Jun 2011 at 5:40

GoogleCodeExporter commented 9 years ago
Thank for your post.
But I needed to do another change in around line 400:
change       if( $this.val() === value )
to        f( value === 'on' )

HTH.

Original comment by seb.ha...@gmail.com on 22 Dec 2011 at 10:13