You can pointto formelements likeweve pointedto everythingelse
But there are some special attributes youcan use to select form elements• :checked • Applies to: Checkboxes and radio buttons • Selects: All that are checked or turned on • Example: $(input[name="networks"]:checked) – All checkboxes with the name "networks" that the user has checked• :selected • Applies to: Listboxes and dropdowns • Selects: All that the user chose • Example: $(#idOfListBox :selected) – All choices in the listbox that the user has chosen
Radiobuttons are weird<input type="radio" name="shipMethod" id="Fedex" value="Fedex"/>!<input type="radio" name="shipMethod" id="UPS" value="UPS"/>!<input type="radio" name="shipMethod" id="USPS" value="USPS"/>!• On a radiobutton, the change() event only fires when the radiobutton turns on.• So dont point to each button:$("#Fedex").change(doSomething());!• Point to the group instead:$("input[name=shipMethod]").change(doSomething());!
An easy way is touse the Validationplug-in1. Include jQuery itself2. Download the validation plug-in3. Add validation rules • class="required" • class="date" • class="digits"4. Add your custom error messages5. $(#myForm).validate()
Conclusion• We can make HTML forms easier to use through jQuery• Use jQuerys special form selectors (:checked, etc.)• Get and set values with val() and prop(checked)• Place the user in the right field with focus()• Hide unneeded options with hide()• Show them again when ready with show()• Validate on the client with the jQuery Validation plug-in