You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ This site consists of content maintained in [Markdown](http://daringfireball.net
23
23
24
24
The entire site is managed via [this Git repository](https://github.com/jquery/learn.jquery.com). If you'd like to contribute new articles, make edits to existing content, or work on the site itself, the first thing you'll need is a [fork](http://help.github.com/fork-a-repo/). When you have changes you'd like to have reviewed and integrated into the site, submit a [pull request](http://help.github.com/send-pull-requests/).
25
25
26
-
If you aren't already familiar with Git, you'll still need a fork and a GitHub account, but you can can edit files directly via [GitHub's in-browser editor](https://github.com/blog/905-edit-like-an-ace), but you won't be able to create new content. We encourage you to [learn how to use Git andGitHub](http://help.github.com/), it'll probably pretty useful no matter what.
26
+
If you aren't already familiar with Git, you'll still need a fork and a GitHub account, but you can can edit files directly via [GitHub's in-browser editor](https://github.com/blog/905-edit-like-an-ace), but you won't be able to create new content. We encourage you to [learn how to use Git and GitHub](http://help.github.com/), it'll probably pretty useful no matter what.
Copy file name to clipboardExpand all lines: content/ajax/ajax-and-forms.md
+76-12
Original file line number
Diff line number
Diff line change
@@ -3,25 +3,89 @@ chapter : ajax
3
3
section : 4
4
4
title : Ajax and Forms
5
5
attribution: jQuery Fundamentals
6
+
6
7
---
7
-
jQuery’s ajax capabilities can be especially useful when dealing with forms.
8
-
The [jQuery Form Plugin](http://jquery.malsup.com/form/) is a well-tested tool
9
-
for adding Ajax capabilities to forms, and you should generally use it for
10
-
handling forms with Ajax rather than trying to roll your own solution for
11
-
anything remotely complex. That said, there are a two jQuery methods you
12
-
should know that relate to form processing in jQuery: `$.fn.serialize` and
13
-
`$.fn.serializeArray`.
8
+
9
+
jQuery’s ajax capabilities can be especially useful when dealing with forms. There are several advantages, which can range from serialization, to simple client-side validation (e.g. "Sorry, that username is taken"), to [prefilters](http://api.jquery.com/extending-ajax/#Prefilters) (explained below), and even more!
10
+
11
+
### Serialization
12
+
Serializing form inputs in jQuery is extremely easy. Two methods come supported natively - `$.fn.serialize` and `$.fn.serializeArray`. While the names are fairly self-explanatory, there are many advantages to using them.
13
+
14
+
The `serialize` method serializes a form's data into a query string. For the element's value to be serialized, it **must** have a `name` attribute. Please noted that values from inputs with a type of `checkbox` or `radio` are included only if they are checked.
15
+
14
16
15
17
<javascriptcaption="Turning form data into a query string">
16
-
$('#myForm').serialize();
18
+
$('#myForm').serialize(); // creates a query string like this: field_1=something&field2=somethingElse
17
19
</javascript>
18
20
21
+
While plain old serialization is great, sometimes your application would work better if you sent over an array of objects, instead of just the query string. For that, jQuery has the `serializeArray` method. It's very similar to the `serialize` method listed above, except it produces an array of objects, instead of a string.
22
+
19
23
<javascriptcaption="Creating an array of objects containing form data">
20
24
$('#myForm').serializeArray();
21
25
22
26
// creates a structure like this:
23
-
[
24
-
{ name : 'field1', value : 123 },
25
-
{ name : 'field2', value : 'hello world' }
26
-
]
27
+
// [
28
+
// { name : 'field_1', value : 'something' },
29
+
// { name : 'field_2', value : 'somethingElse' }
30
+
// ]
31
+
</javascript>
32
+
33
+
### Client-side validation
34
+
Client-side validation is, much like many other things, extremely easy using jQuery. While there are several cases developers can test for, some of the most common ones are: presence of a required input, valid usernames/emails/phone numbers/etc..., or checking an "I agree..." box.
35
+
36
+
Please note that it is advisable that you also perform server-side validation for your inputs. However, it typically makes for a better user experience to be able to validate some things without submitting the form.
37
+
38
+
With that being said, let's jump on in to some examples! First, we'll see how easy it is to check if a required field doesn't have anything in it. If it doesn't, then we'll `return false`, and prevent the form from processing.
39
+
40
+
<javascriptcaption="Using validation to check for the presence of an input">
41
+
$("#form").submit(function( e ) {
42
+
if ( $(".required").val().length === 0 ) { // if .required's value's length is zero
43
+
// usually show some kind of error message here
44
+
return false; // this prevents the form from submitting
45
+
}
46
+
else {
47
+
// run $.ajax here
48
+
}
49
+
});
27
50
</javascript>
51
+
52
+
Let's see how easy it is to check for invalid characters in a username:
53
+
54
+
<javascriptcaption="Validate a phone number field">
55
+
$("#form").submit(function( e ) {
56
+
var inputtedPhoneNumber = $("#phone").val()
57
+
, phoneNumberRegex = ^\d*$/; // match only numbers
58
+
59
+
if ( !phoneNumberRegex.test( inputtedPhoneNumber ) ) { // if the phone number doesn't match the regex
60
+
// usually show some kind of error message ere
61
+
return false; // prevent the form from submitting
62
+
}
63
+
else {
64
+
// run $.ajax here
65
+
}
66
+
})
67
+
</javascript>
68
+
69
+
70
+
71
+
### Prefiltering
72
+
A prefilter is a way to modify the ajax options before each request is sent (hence, the name `prefilter`).
73
+
74
+
For example, say we would like to modify all crossDomain requests through a proxy. To do so with a prefilter is quite simple:
75
+
76
+
<javascriptcaption="Using a proxy with a prefilter">
You can pass in an optional argument before the callback function that specifies which `dataTypes` you'd like the prefilter to be applied to. For example, if we want our prefilter to only apply to `JSON` and `script` requests, we'd do:
86
+
87
+
<javascriptcaption="using the optional dataTypes argument">
Copy file name to clipboardExpand all lines: content/jquery-basics/css-styling-dimensions.md
+4-2
Original file line number
Diff line number
Diff line change
@@ -31,6 +31,8 @@ contains multiple properties. This is a common way to pass multiple arguments
31
31
to a function, and many jQuery setter methods accept objects to set mulitple
32
32
values at once.
33
33
34
+
**Note:** while we do not recommend using `$.fn.css` as a setter in production-ready code (see below), when passing in an object to set CSS, CSS properties will be camelCased, instead of using a hypen.
35
+
34
36
### Using CSS Classes for Styling
35
37
36
38
As a getter, the `$.fn.css` method is valuable; however, it should generally be
@@ -39,15 +41,15 @@ presentational information in your JavaScript. Instead, write CSS rules for
39
41
classes that describe the various visual states, and then simply change the
40
42
class on the element you want to affect.
41
43
42
-
<javscriptcaption="Working with classes">
44
+
<javascriptcaption="Working with classes">
43
45
var $h1 = $('h1');
44
46
45
47
$h1.addClass('big');
46
48
$h1.removeClass('big');
47
49
$h1.toggleClass('big');
48
50
49
51
if ($h1.hasClass('big')) { ... }
50
-
</javscript>
52
+
</javascript>
51
53
52
54
Classes can also be useful for storing state information about an element, such as indicating that an element is selected.
0 commit comments