views:

29

answers:

1

From firebug:

>>> $("form#commentform").serialize();
"username=&email=&comment=&verify="
>>> $("#commentform").serialize();
""
>>> $("form#commentform")
[form#commentform]
>>> $("#commentform")
[div#commentform]
>>> $("#commentform").length
1

This is really weird,form#commentform works,but #commentform doesn't,why?

+2  A: 

You have two elements with the id commentform.
This is invalid. $('#commentform') returns only the first one, which cannot be serialized.

From the specs:

Matches a single element with the given id attribute.

Kobi
I just tried `>>> $("#commentform").length`,which outputs 1.
That is exactly the point - it assumes there's only one item. Under the hood, btw, it is using `getElementById`, which returns a single element.
Kobi