AJAX response will replace this content.
124 |
125 | ---
126 |
127 | ### ajaxSubmit
128 |
129 | The following code controls the HTML form beneath it. It uses `ajaxSubmit` to submit the form.
130 | ```javascript
131 | // prepare the form when the DOM is ready
132 | $(document).ready(function() {
133 | var options = {
134 | target: '#output2', // target element(s) to be updated with server response
135 | beforeSubmit: showRequest, // pre-submit callback
136 | success: showResponse // post-submit callback
137 |
138 | // other available options:
139 | //url: url // override for form's 'action' attribute
140 | //type: type // 'get' or 'post', override for form's 'method' attribute
141 | //dataType: null // 'xml', 'script', or 'json' (expected server response type)
142 | //clearForm: true // clear all form fields after successful submit
143 | //resetForm: true // reset the form after successful submit
144 |
145 | // $.ajax options can be used here too, for example:
146 | //timeout: 3000
147 | };
148 |
149 | // bind to the form's submit event
150 | $('#myForm2').submit(function() {
151 | // inside event callbacks 'this' is the DOM element so we first
152 | // wrap it in a jQuery object and then invoke ajaxSubmit
153 | $(this).ajaxSubmit(options);
154 |
155 | // !!! Important !!!
156 | // always return false to prevent standard browser submit and page navigation
157 | return false;
158 | });
159 | });
160 |
161 | // pre-submit callback
162 | function showRequest(formData, jqForm, options) {
163 | // formData is an array; here we use $.param to convert it to a string to display it
164 | // but the form plugin does this for you automatically when it submits the data
165 | var queryString = $.param(formData);
166 |
167 | // jqForm is a jQuery object encapsulating the form element. To access the
168 | // DOM element for the form do this:
169 | // var formElement = jqForm[0];
170 |
171 | alert('About to submit: \n\n' + queryString);
172 |
173 | // here we could return false to prevent the form from being submitted;
174 | // returning anything other than false will allow the form submit to continue
175 | return true;
176 | }
177 |
178 | // post-submit callback
179 | function showResponse(responseText, statusText, xhr, $form) {
180 | // for normal html responses, the first argument to the success callback
181 | // is the XMLHttpRequest object's responseText property
182 |
183 | // if the ajaxSubmit method was passed an Options Object with the dataType
184 | // property set to 'xml' then the first argument to the success callback
185 | // is the XMLHttpRequest object's responseXML property
186 |
187 | // if the ajaxSubmit method was passed an Options Object with the dataType
188 | // property set to 'json' then the first argument to the success callback
189 | // is the json data object returned by the server
190 |
191 | alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
192 | '\n\nThe output div should have already been updated with the responseText.');
193 | }
194 | ```
195 |
196 | AJAX response will replace this content.
249 |
250 |
251 | ---
252 |
253 | ### Validation
254 | This section gives several examples of how form data can be validated before it is sent to the server. The secret is in the `beforeSubmit` option. If this pre-submit callback returns false, the submit process is aborted.
255 |
256 | The following login form is used for each of the examples that follow. Each example will validate that both the *username* and *password* fields have been filled in by the user.
257 |
258 | #### Form Markup:
259 |
260 | ```html
261 |
2 |
3 | Lorem ipsum dolor sit amet
4 |
5 |
9 |
10 | Lorem ipsum dolor sit amet
11 |
12 |
16 |
17 |
24 |
25 | Lorem ipsum dolor sit amet
26 |
27 |
28 |
--------------------------------------------------------------------------------
/test/ajax/json.json:
--------------------------------------------------------------------------------
1 | { "name": "jquery-test" }
--------------------------------------------------------------------------------
/test/ajax/json.txt:
--------------------------------------------------------------------------------
1 | { "name": "jquery-test" }
--------------------------------------------------------------------------------
/test/ajax/script.txt:
--------------------------------------------------------------------------------
1 | formScriptTest = function() {
2 | // no-op
3 | }
4 |
--------------------------------------------------------------------------------
/test/ajax/test.xml:
--------------------------------------------------------------------------------
1 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
224 |
225 |
226 |
227 |
228 |
234 |
235 |
236 |
--------------------------------------------------------------------------------
/test/test.js:
--------------------------------------------------------------------------------
1 | /* global chai, scriptCount */
2 |
3 | 'use strict';
4 |
5 | // helper method
6 | var arrayCount = function(arr, key) {
7 | var count = 0;
8 | for (var i = 0; i < arr.length; i++) {
9 | if (arr[i].name === key) {
10 | count++;
11 | }
12 | }
13 | return count;
14 | };
15 |
16 | // helper method
17 | var arrayValue = function(arr, key) {
18 | for (var i = 0; i < arr.length; i++) {
19 | if (arr[i].name === key) {
20 | return arr[i].value;
21 | }
22 | }
23 | return undefined;
24 | };
25 |
26 |
27 | var assert = chai.assert;
28 | var fixture;
29 |
30 | describe('form', function() {
31 | before(function() {
32 | fixture = $('#main').html();
33 | });
34 |
35 | beforeEach(function() {
36 | $('#main').html(fixture);
37 | });
38 |
39 |
40 | it('"action" and "method" form attributes', function() {
41 | var f = $('#form1');
42 | assert.strictEqual(f.attr('action'), 'ajax/text.html', 'form "action"');
43 | assert.strictEqual(f.attr('method'), 'get', 'form "method"');
44 | });
45 |
46 | it('formToArray: multi-select', function() {
47 | var a = $('#form1').formToArray();
48 | assert.strictEqual(a.constructor, Array, 'type check');
49 | assert.strictEqual(a.length, 13, 'array length');
50 | assert.strictEqual(arrayCount(a, 'Multiple'), 3, 'multi-select');
51 | });
52 |
53 | it('formToArray: "action" and "method" inputs', function() {
54 | var a = $('#form1').formToArray();
55 | assert.strictEqual(a.constructor, Array, 'type check');
56 | assert.strictEqual(arrayValue(a, 'action'), '1', 'input name=action');
57 | assert.strictEqual(arrayValue(a, 'method'), '2', 'input name=method');
58 | });
59 |
60 | it('formToArray: semantic test', function() {
61 | var formData = $('#form2').formToArray(true);
62 | var testData = ['a','b','c','d','e','f'];
63 | for (var i = 0; i < 6; i++) {
64 | assert.strictEqual(formData[i].name, testData[i], 'match value at index=' + i);
65 | }
66 | });
67 |
68 | it('formToArray: text promotion for missing value attributes', function() {
69 | var expected = [
70 | { name: 'A', value: ''},
71 | { name: 'B', value: 'MISSING_ATTR'},
72 | { name: 'C', value: ''},
73 | { name: 'C', value: 'MISSING_ATTR'}
74 | ];
75 | var a = $('#form6').formToArray(true);
76 |
77 | // verify all the option values
78 | for (var i = 0; i < a.length; i++) {
79 | assert.strictEqual(a[i].name, expected[i].name, 'Name: ' + a[i].name + ' = ' + expected[i].name);
80 | assert.strictEqual(a[i].value, expected[i].value, 'Value: ' + a[i].value + ' = ' + expected[i].value);
81 | }
82 | });
83 |
84 | it('formToArray: outside fields', function() {
85 | var formData = $('#form10').formToArray();
86 | assert.strictEqual(formData.length, 2, 'There are two "successful" elements of the form');
87 | });
88 |
89 | // test string serialization
90 | it('serialize: param count', function() {
91 | var s = $('#form1').formSerialize();
92 | assert.ok(s.constructor == String, 'type check');
93 | assert.ok(s.split('&').length == 13, 'string array length');
94 | });
95 |
96 | // test support for input elements not contained within a form
97 | it('serialize: pseudo form', function() {
98 | var s = $('#pseudo *').fieldSerialize();
99 | assert.ok(s.constructor == String, 'type check');
100 | assert.ok(s.split('&').length == 3, 'string array length');
101 | });
102 |
103 |
104 | // test resetForm
105 | it('resetForm (text input)', function() {
106 | var $el = $('#form1 input[name=Name]');
107 | var val = $el.val();
108 | assert.ok('MyName1' == val, 'beforeSubmit: ' + val);
109 | $el.val('test');
110 | val = $el.val();
111 | assert.ok('test' == $el.val(), 'update: ' + val);
112 | $('#form1').resetForm();
113 | val = $el.val();
114 | assert.ok('MyName1' == val, 'success: ' + val);
115 | });
116 |
117 | // test resetForm
118 | it('resetForm (select)', function() {
119 | var $el = $('#form1 select[name=Single]');
120 | var val = $el.val();
121 | assert.ok('one' == val, 'beforeSubmit: ' + val);
122 | $el.val('two');
123 | val = $el.val();
124 | assert.ok('two' == $el.val(), 'update: ' + val);
125 | $('#form1').resetForm();
126 | val = $el.val();
127 | assert.ok('one' == val, 'success: ' + val);
128 | });
129 |
130 | // test resetForm
131 | it('resetForm (textarea)', function() {
132 | var $el = $('#form1 textarea');
133 | var val = $el.val();
134 | assert.ok('This is Form1' == val, 'beforeSubmit: ' + val);
135 | $el.val('test');
136 | val = $el.val();
137 | assert.ok('test' == val, 'udpate: ' + val);
138 | $('#form1').resetForm();
139 | val = $el.val();
140 | assert.ok('This is Form1' == val, 'success: ' + val);
141 | });
142 |
143 | // test resetForm
144 | it('resetForm (checkbox)', function() {
145 | var el = $('#form1 input:checkbox:checked')[0];
146 | var val = el.value;
147 | assert.ok(el.checked, 'beforeSubmit: ' + el.checked);
148 | el.checked = false;
149 | assert.ok(!el.checked, 'update: ' + el.checked);
150 | $('#form1').resetForm();
151 | assert.ok(el.checked, 'success: ' + el.checked);
152 | });
153 |
154 | // test resetForm
155 | it('resetForm (radio)', function() {
156 | var el = $('#form1 input:radio:checked')[0];
157 | var val = el.value;
158 | assert.ok(el.checked, 'beforeSubmit: ' + el.checked);
159 | el.checked = false;
160 | assert.ok(!el.checked, 'update: ' + el.checked);
161 | $('#form1').resetForm();
162 | assert.ok(el.checked, 'success: ' + el.checked);
163 | });
164 |
165 |
166 | // test clearForm
167 | it('clearForm (text input)', function() {
168 | var $el = $('#form1 input[name=Name]');
169 | var val = $el.val();
170 | assert.ok('MyName1' == val, 'beforeSubmit: ' + val);
171 | $('#form1').clearForm();
172 | val = $el.val();
173 | assert.ok('' == val, 'success: ' + val);
174 | });
175 |
176 | // test clearForm
177 | it('clearForm (select)', function() {
178 | var $el = $('#form1 select[name=Single]');
179 | var val = $el.val();
180 | assert.ok('one' == val, 'beforeSubmit: ' + val);
181 | $('#form1').clearForm();
182 | val = $el.val();
183 | assert.ok(!val, 'success: ' + val);
184 | });
185 |
186 | // test clearForm; here we're testing that a hidden field is NOT cleared
187 | it('clearForm: (hidden input)', function() {
188 | var $el = $('#form1 input:hidden');
189 | var val = $el.val();
190 | assert.ok('hiddenValue' == val, 'beforeSubmit: ' + val);
191 | $('#form1').clearForm();
192 | val = $el.val();
193 | assert.ok('hiddenValue' == val, 'success: ' + val);
194 | });
195 |
196 |
197 | // test clearForm; here we're testing that a submit element is NOT cleared
198 | it('clearForm: (submit input)', function() {
199 | var $el = $('#form1 input:submit');
200 | var val = $el.val();
201 | assert.ok('Submit1' == val, 'beforeSubmit: ' + val);
202 | $('#form1').clearForm();
203 | val = $el.val();
204 | assert.ok('Submit1' == val, 'success: ' + val);
205 | });
206 |
207 | // test clearForm
208 | it('clearForm (checkbox)', function() {
209 | var el = $('#form1 input:checkbox:checked')[0];
210 | assert.ok(el.checked, 'beforeSubmit: ' + el.checked);
211 | $('#form1').clearForm();
212 | assert.ok(!el.checked, 'success: ' + el.checked);
213 | });
214 |
215 | // test clearForm
216 | it('clearForm (radio)', function() {
217 | var el = $('#form1 input:radio:checked')[0];
218 | assert.ok(el.checked, 'beforeSubmit: ' + el.checked);
219 | $('#form1').clearForm();
220 | assert.ok(!el.checked, 'success: ' + el.checked);
221 | });
222 |
223 | // test ajaxSubmit target update
224 | it('ajaxSubmit: target == String', function() {
225 | $('#targetDiv').empty();
226 | // stop();
227 | var opts = {
228 | target: '#targetDiv',
229 | success: function() { // post-callback
230 | assert.ok(true, 'post-callback');
231 | assert.ok($('#targetDiv').text().match('Lorem ipsum'), 'targetDiv updated');
232 | // start();
233 | }
234 | };
235 |
236 | // expect(2);
237 | $('#form3').ajaxSubmit(opts);
238 | });
239 |
240 | // test passing jQuery object as the target
241 | it('ajaxSubmit: target == jQuery object', function() {
242 | // stop();
243 | var target = $('#targetDiv');
244 | target.empty();
245 |
246 | var opts = {
247 | target: target,
248 | success: function(responseText) { // post-callback
249 | assert.ok(true, 'post-callback');
250 | assert.ok($('#targetDiv').text().match('Lorem ipsum'), 'targetDiv updated');
251 | // start();
252 | }
253 | };
254 |
255 | // expect(2);
256 | $('#form2').ajaxSubmit(opts);
257 | });
258 |
259 | // test passing DOM element as the target
260 | it('ajaxSubmit: target == DOM element', function() {
261 | // stop();
262 | $('#targetDiv').empty();
263 | var el = $('#targetDiv')[0];
264 |
265 | var opts = {
266 | target: '#targetDiv',
267 | success: function(responseText) { // post-callback
268 | assert.ok(true, 'post-callback');
269 | assert.ok($('#targetDiv').text().match('Lorem ipsum'), 'targetDiv updated');
270 | // start();
271 | }
272 | };
273 |
274 | // expect(2);
275 | $('#form2').ajaxSubmit(opts);
276 | });
277 |
278 | // test simulated $.load behavior
279 | it('ajaxSubmit: load target with scripts', function() {
280 | // stop();
281 | $('#targetDiv').empty();
282 |
283 | var opts = {
284 | target: '#targetDiv',
285 | url: 'ajax/doc-with-scripts.html?' + new Date().getTime(),
286 | success: function(responseText) { // post-callback
287 | assert.ok(true, 'success-callback');
288 | assert.ok($('#targetDiv').text().match('Lorem ipsum'), 'targetDiv updated');
289 | assert.ok(typeof unitTestVariable1 != 'undefined', 'first script block executed');
290 | assert.ok(typeof unitTestVariable2 != 'undefined', 'second script block executed');
291 | assert.ok(typeof scriptCount != 'undefined', 'third script block executed');
292 | assert.ok(scriptCount == 1, 'scripts executed once: ' + scriptCount);
293 | // start();
294 | }
295 | };
296 |
297 | // expect(6);
298 | $('#form2').ajaxSubmit(opts);
299 | });
300 |
301 | // test ajaxSubmit pre-submit callback
302 | it('ajaxSubmit: pre-submit callback', function() {
303 | var opts = {
304 | beforeSubmit: function(a, jq) { // pre-submit callback
305 | assert.ok(true, 'pre-submit callback');
306 | assert.ok(a.constructor == Array, 'type check array');
307 | assert.ok(jq.jquery, 'type check jQuery');
308 | assert.ok(jq[0].tagName.toLowerCase() == 'form', 'jQuery arg == "form": ' + jq[0].tagName.toLowerCase());
309 | }
310 | };
311 |
312 | // expect(4);
313 | $('#form3').ajaxSubmit(opts);
314 | });
315 |
316 | // test ajaxSubmit post-submit callback for response and status text
317 | it('ajaxSubmit: post-submit callback', function() {
318 | // stop();
319 |
320 | var opts = {
321 | success: function(responseText, statusText) { // post-submit callback
322 | assert.ok(true, 'post-submit callback');
323 | assert.ok(responseText.match('Lorem ipsum'), 'responseText');
324 | assert.ok(statusText == 'success', 'statusText');
325 | // start();
326 | }
327 | };
328 |
329 | // expect(3);
330 | $('#form3').ajaxSubmit(opts);
331 | });
332 |
333 | // test ajaxSubmit with function argument
334 | it('ajaxSubmit: function arg', function() {
335 | // stop();
336 |
337 | // expect(1);
338 | $('#form3').ajaxSubmit(function() {
339 | assert.ok(true, 'callback hit');
340 | // start();
341 | });
342 | });
343 |
344 | // test semantic support via ajaxSubmit's pre-submit callback
345 | it('ajaxSubmit: semantic test', function() {
346 | var testData = ['a','b','c','d','e','f'];
347 |
348 | var opts = {
349 | semantic: true,
350 | beforeSubmit: function(a, jq) { // pre-submit callback
351 | assert.ok(true, 'pre-submit callback');
352 | assert.ok(a.constructor == Array, 'type check');
353 | assert.ok(jq.jquery, 'type check jQuery');
354 | for (var i = 0; i < a.length; i++) {
355 | assert.ok(a[i].name == testData[i], 'match value at index=' + i);
356 | }
357 | }
358 | };
359 |
360 | // expect(9);
361 | $('#form2').ajaxSubmit(opts);
362 | });
363 |
364 | // test json datatype
365 | it('ajaxSubmit: dataType == json', function() {
366 | // stop();
367 |
368 | var opts = {
369 | url: 'ajax/json.json',
370 | dataType: 'json',
371 | success: function(data, statusText) { // post-submit callback
372 | // assert that the json data was evaluated
373 | assert.ok(typeof data == 'object', 'json data type');
374 | assert.ok(data.name == 'jquery-test', 'json data contents');
375 | // start();
376 | }
377 | };
378 |
379 | // expect(2);
380 | $('#form2').ajaxSubmit(opts);
381 | });
382 |
383 |
384 | // test script datatype
385 | it('ajaxSubmit: dataType == script', function() {
386 | // stop();
387 |
388 | var opts = {
389 | url: 'ajax/script.txt?' + new Date().getTime(), // don't let ie cache it
390 | dataType: 'script',
391 | success: function(responseText, statusText) { // post-submit callback
392 | assert.ok(typeof formScriptTest == 'function', 'script evaluated');
393 | assert.ok(responseText.match('formScriptTest'), 'script returned');
394 | // start();
395 | }
396 | };
397 |
398 | // expect(2);
399 | $('#form2').ajaxSubmit(opts);
400 | });
401 |
402 | // test xml datatype
403 | it('ajaxSubmit: dataType == xml', function() {
404 | // stop();
405 |
406 | var opts = {
407 | url: 'ajax/test.xml',
408 | dataType: 'xml',
409 | success: function(responseXML, statusText) { // post-submit callback
410 | assert.ok(typeof responseXML == 'object', 'data type xml');
411 | assert.ok($('test', responseXML).length == 3, 'xml data query');
412 | // start();
413 | }
414 | };
415 |
416 | // expect(2);
417 | $('#form2').ajaxSubmit(opts);
418 | });
419 |
420 |
421 | // test that args embedded in the action are honored; no real way
422 | // to assert this so successful callback is used to signal success
423 | it('ajaxSubmit: existing args in action attr', function() {
424 | // stop();
425 |
426 | var opts = {
427 | success: function() { // post-submit callback
428 | assert.ok(true, 'post callback');
429 | // start();
430 | }
431 | };
432 |
433 | // expect(1);
434 | $('#form5').ajaxSubmit(opts);
435 | });
436 |
437 | // test ajaxSubmit using pre-submit callback to cancel submit
438 | it('ajaxSubmit: cancel submit', function() {
439 |
440 | var opts = {
441 | beforeSubmit: function(a, jq) { // pre-submit callback
442 | assert.ok(true, 'pre-submit callback');
443 | assert.ok(a.constructor == Array, 'type check');
444 | assert.ok(jq.jquery, 'type check jQuery');
445 | return false; // return false to abort submit
446 | },
447 | success: function() { // post-submit callback
448 | assert.ok(false, 'should not hit this post-submit callback');
449 | }
450 | };
451 |
452 | // expect(3);
453 | $('#form3').ajaxSubmit(opts);
454 | });
455 |
456 | // test submitting a pseudo-form
457 | it('ajaxSubmit: pseudo-form', function() {
458 | // stop();
459 |
460 | var opts = {
461 | beforeSubmit: function(a, jq) { // pre-submit callback
462 | assert.ok(true, 'pre-submit callback');
463 | assert.ok(a.constructor == Array, 'type check');
464 | assert.ok(jq.jquery, 'type check jQuery');
465 | assert.ok(jq[0].tagName.toLowerCase() == 'div', 'jQuery arg == "div"');
466 | },
467 | success: function() { // post-submit callback
468 | assert.ok(true, 'post-submit callback');
469 | // start();
470 | },
471 | // url and method must be provided for a pseudo form since they can
472 | // not be extracted from the markup
473 | url: 'ajax/text.html',
474 | type: 'post'
475 | };
476 |
477 | // expect(5);
478 | $('#pseudo').ajaxSubmit(opts);
479 | });
480 |
481 | // test eval of json response
482 | it('ajaxSubmit: evaluate response', function() {
483 | // stop();
484 |
485 | var opts = {
486 | success: function(responseText) { // post-callback
487 | assert.ok(true, 'post-callback');
488 | var data = eval.call(window, '(' + responseText + ')');
489 | assert.ok(data.name == 'jquery-test', 'evaled response');
490 | // start();
491 | },
492 | url: 'ajax/json.txt'
493 | };
494 |
495 | // expect(2);
496 | $('#form2').ajaxSubmit(opts);
497 | });
498 |
499 |
500 | // test pre and post callbacks for ajaxForm
501 | it('ajaxForm: pre and post callbacks', function() {
502 | // stop();
503 |
504 | var opts = {
505 | beforeSubmit: function(a, jq) { // pre-submit callback
506 | assert.ok(true, 'pre-submit callback');
507 | assert.ok(a.constructor == Array, 'type check');
508 | assert.ok(jq.jquery, 'type check jQuery');
509 | },
510 | success: function() { // post-submit callback
511 | assert.ok(true, 'post-submit callback');
512 | // start();
513 | }
514 | };
515 |
516 | // expect(4);
517 | $('#form4').ajaxForm(opts);
518 | $('#submitForm4')[0].click(); // trigger the submit button
519 | });
520 |
521 | // test that the value of the submit button is captured
522 | it('ajaxForm: capture submit element', function() {
523 |
524 | var opts = {
525 | beforeSubmit: function(a, jq) { // pre-callback
526 | assert.ok(true, 'pre-callback');
527 | assert.ok(a.constructor == Array, 'type check');
528 | assert.ok(jq.jquery, 'type check jQuery');
529 | assert.ok(arrayValue(a, 'form4inputName') !== null, 'submit button');
530 | }
531 | };
532 |
533 | // expect(4);
534 | $('#form4').ajaxForm(opts);
535 | $('#submitForm4withName')[0].click();
536 | });
537 |
538 | // test image submit support
539 | it('ajaxForm: capture submit image coordinates', function() {
540 |
541 | var opts = {
542 | beforeSubmit: function(a, jq) { // pre-callback
543 | assert.ok(true, 'pre-callback');
544 | assert.ok(a.constructor == Array, 'type check');
545 | assert.ok(jq.jquery, 'type check jQuery');
546 | assert.ok(arrayValue(a, 'myImage.x') !== null, 'x coord');
547 | assert.ok(arrayValue(a, 'myImage.y') !== null, 'y coord');
548 | }
549 | };
550 |
551 | // expect(5);
552 | $('#form4').ajaxForm(opts);
553 | $('#form4imageSubmit')[0].click();
554 | });
555 |
556 | // test image submit support
557 | it('ajaxForm: capture submit image coordinates (semantic=true)', function() {
558 |
559 | var opts = {
560 | semantic: true,
561 | beforeSubmit: function(a, jq) { // pre-callback
562 | assert.ok(true, 'pre-callback');
563 | assert.ok(a.constructor == Array, 'type check');
564 | assert.ok(jq.jquery, 'type check jQuery');
565 | assert.ok(arrayValue(a, 'myImage.x') !== null, 'x coord');
566 | assert.ok(arrayValue(a, 'myImage.y') !== null, 'y coord');
567 | }
568 | };
569 |
570 | // expect(5);
571 | $('#form4').ajaxForm(opts);
572 | $('#form4imageSubmit')[0].click();
573 | });
574 |
575 | // test that the targetDiv gets updated
576 | it('ajaxForm: update target div', function() {
577 | $('#targetDiv').empty();
578 | // stop();
579 |
580 | var opts = {
581 | target: '#targetDiv',
582 | beforeSubmit: function(a, jq) { // pre-callback
583 | assert.ok(true, 'pre-callback');
584 | assert.ok(a.constructor == Array, 'type check');
585 | assert.ok(jq.jquery, 'type check jQuery');
586 | },
587 | success: function() {
588 | assert.ok(true, 'post-callback');
589 | assert.ok($('#targetDiv').text().match('Lorem ipsum'), 'targetDiv updated');
590 | // start();
591 | }
592 | };
593 |
594 | // expect(5);
595 | $('#form4').ajaxForm(opts);
596 | $('#submitForm4')[0].click();
597 | });
598 |
599 | it('"success" callback', function() {
600 | $('#targetDiv').empty();
601 | // stop();
602 |
603 | var opts = {
604 | success: function() {
605 | assert.ok(true, 'post-callback');
606 | // start();
607 | }
608 | };
609 |
610 | // expect(1);
611 | $('#form3').ajaxSubmit(opts);
612 | });
613 |
614 | it('"error" callback', function() {
615 | $('#targetDiv').empty();
616 | // stop();
617 |
618 | var opts = {
619 | url: 'ajax/404.html',
620 | error: function() {
621 | assert.ok(true, 'error-callback');
622 | // start();
623 | },
624 | success: function() { // post-submit callback
625 | assert.ok(false, 'should not hit post-submit callback');
626 | }
627 | };
628 |
629 | // expect(1);
630 | $('#form3').ajaxSubmit(opts);
631 | });
632 |
633 |
634 | it('fieldValue(true)', function() {
635 | var i;
636 |
637 | assert.ok('5' == $('#fieldTest input').fieldValue(true)[0], 'input');
638 | assert.ok('1' == $('#fieldTest :input').fieldValue(true)[0], ':input');
639 | assert.ok('5' == $('#fieldTest input:hidden').fieldValue(true)[0], ':hidden');
640 | assert.ok('14' == $('#fieldTest :password').fieldValue(true)[0], ':password');
641 | assert.ok('12' == $('#fieldTest :radio').fieldValue(true)[0], ':radio');
642 | assert.ok('1' == $('#fieldTest select').fieldValue(true)[0], 'select');
643 |
644 | var expected = ['8','10'];
645 | var result = $('#fieldTest :checkbox').fieldValue(true);
646 | assert.ok(result.length == expected.length, 'result size check (checkbox): ' + result.length + '=' + expected.length);
647 | for (i = 0; i < result.length; i++) {
648 | assert.ok(result[i] == expected[i], expected[i]);
649 | }
650 |
651 | expected = ['3','4'];
652 | result = $('#fieldTest [name=B]').fieldValue(true);
653 | assert.ok(result.length == expected.length, 'result size check (select-multiple): ' + result.length + '=' + expected.length);
654 | for (i = 0; i < result.length; i++) {
655 | assert.ok(result[i] == expected[i], expected[i]);
656 | }
657 | });
658 |
659 | it('fieldValue(false)', function() {
660 | var i;
661 |
662 | assert.ok('5' == $('#fieldTest input').fieldValue(false)[0], 'input');
663 | assert.ok('1' == $('#fieldTest :input').fieldValue(false)[0], ':input');
664 | assert.ok('5' == $('#fieldTest input:hidden').fieldValue(false)[0], ':hidden');
665 | assert.ok('14' == $('#fieldTest :password').fieldValue(false)[0], ':password');
666 | assert.ok('1' == $('#fieldTest select').fieldValue(false)[0], 'select');
667 |
668 | var expected = ['8','9','10'];
669 | var result = $('#fieldTest :checkbox').fieldValue(false);
670 | assert.ok(result.length == expected.length, 'result size check (checkbox): ' + result.length + '=' + expected.length);
671 | for (i = 0; i < result.length; i++) {
672 | assert.ok(result[i] == expected[i], expected[i]);
673 | }
674 |
675 | expected = ['11','12','13'];
676 | result = $('#fieldTest :radio').fieldValue(false);
677 | assert.ok(result.length == expected.length, 'result size check (radio): ' + result.length + '=' + expected.length);
678 | for (i = 0; i < result.length; i++) {
679 | assert.ok(result[i] == expected[i], expected[i]);
680 | }
681 |
682 | expected = ['3','4'];
683 | result = $('#fieldTest [name=B]').fieldValue(false);
684 | assert.ok(result.length == expected.length, 'result size check (select-multiple): ' + result.length + '=' + expected.length);
685 | for (i = 0; i < result.length; i++) {
686 | assert.ok(result[i] == expected[i], expected[i]);
687 | }
688 | });
689 |
690 | it('fieldSerialize(true) input', function() {
691 | var expected = ['C=5', 'D=6', 'F=8', 'F=10', 'G=12', 'H=14'];
692 |
693 | var result = $('#fieldTest input').fieldSerialize(true);
694 | result = result.split('&');
695 |
696 | assert.ok(result.length == expected.length, 'result size check: ' + result.length + '=' + expected.length);
697 | for (var i = 0; i < result.length; i++) {
698 | assert.ok(result[i] == expected[i], expected[i] + ' = ' + result[i]);
699 | }
700 | });
701 |
702 | it('fieldSerialize(true) :input', function() {
703 | var expected = ['A=1','B=3','B=4','C=5','D=6','E=7','F=8','F=10','G=12','H=14'];
704 |
705 | var result = $('#fieldTest :input').fieldSerialize(true);
706 | result = result.split('&');
707 |
708 | assert.ok(result.length == expected.length, 'result size check: ' + result.length + '=' + expected.length);
709 | for (var i = 0; i < result.length; i++) {
710 | assert.ok(result[i] == expected[i], expected[i] + ' = ' + result[i]);
711 | }
712 | });
713 |
714 | it('fieldSerialize(false) :input', function() {
715 | var expected = ['A=1','B=3','B=4','C=5','D=6','E=7','F=8','F=9','F=10','G=11','G=12','G=13','H=14','I=15','J=16'];
716 |
717 | var result = $('#fieldTest :input').fieldSerialize(false);
718 | result = result.split('&');
719 |
720 | assert.ok(result.length == expected.length, 'result size check: ' + result.length + '=' + expected.length);
721 | for (var i = 0; i < result.length; i++) {
722 | assert.ok(result[i] == expected[i], expected[i] + ' = ' + result[i]);
723 | }
724 | });
725 |
726 | it('fieldSerialize(true) select-mulitple', function() {
727 | var expected = ['B=3','B=4'];
728 |
729 | var result = $('#fieldTest [name=B]').fieldSerialize(true);
730 | result = result.split('&');
731 |
732 | assert.ok(result.length == expected.length, 'result size check: ' + result.length + '=' + expected.length);
733 | for (var i = 0; i < result.length; i++) {
734 | assert.ok(result[i] == expected[i], expected[i] + ' = ' + result[i]);
735 | }
736 | });
737 |
738 | it('fieldSerialize(true) :checkbox', function() {
739 | var expected = ['F=8','F=10'];
740 |
741 | var result = $('#fieldTest :checkbox').fieldSerialize(true);
742 | result = result.split('&');
743 |
744 | assert.ok(result.length == expected.length, 'result size check: ' + result.length + '=' + expected.length);
745 | for (var i = 0; i < result.length; i++) {
746 | assert.ok(result[i] == expected[i], expected[i] + ' = ' + result[i]);
747 | }
748 | });
749 |
750 | it('fieldSerialize(false) :checkbox', function() {
751 | var expected = ['F=8','F=9','F=10'];
752 |
753 | var result = $('#fieldTest :checkbox').fieldSerialize(false);
754 | result = result.split('&');
755 |
756 | assert.ok(result.length == expected.length, 'result size check: ' + result.length + '=' + expected.length);
757 | for (var i = 0; i < result.length; i++) {
758 | assert.ok(result[i] == expected[i], expected[i] + ' = ' + result[i]);
759 | }
760 | });
761 |
762 | it('fieldSerialize(true) :radio', function() {
763 | var expected = ['G=12'];
764 |
765 | var result = $('#fieldTest :radio').fieldSerialize(true);
766 | result = result.split('&');
767 |
768 | assert.ok(result.length == expected.length, 'result size check: ' + result.length + '=' + expected.length);
769 | for (var i = 0; i < result.length; i++) {
770 | assert.ok(result[i] == expected[i], expected[i] + ' = ' + result[i]);
771 | }
772 | });
773 |
774 | it('fieldSerialize(false) :radio', function() {
775 | var expected = ['G=11','G=12','G=13'];
776 |
777 | var result = $('#fieldTest :radio').fieldSerialize(false);
778 | result = result.split('&');
779 |
780 | assert.ok(result.length == expected.length, 'result size check: ' + result.length + '=' + expected.length);
781 | for (var i = 0; i < result.length; i++) {
782 | assert.ok(result[i] == expected[i], expected[i] + ' = ' + result[i]);
783 | }
784 | });
785 |
786 | it('ajaxForm - auto unbind', function() {
787 | $('#targetDiv').empty();
788 | // stop();
789 |
790 | var opts = {
791 | target: '#targetDiv',
792 | beforeSubmit: function(a, jq) { // pre-callback
793 | assert.ok(true, 'pre-callback');
794 | },
795 | success: function() {
796 | assert.ok(true, 'post-callback');
797 | // start();
798 | }
799 | };
800 |
801 | // expect(2);
802 | // multiple binds
803 | $('#form8').ajaxForm(opts).ajaxForm(opts).ajaxForm(opts);
804 | $('#submitForm8')[0].click();
805 | });
806 |
807 | it('ajaxFormUnbind', function() {
808 | $('#targetDiv').empty();
809 | // stop();
810 |
811 | var opts = {
812 | target: '#targetDiv',
813 | beforeSubmit: function(a, jq) { // pre-callback
814 | assert.ok(true, 'pre-callback');
815 | },
816 | success: function() {
817 | assert.ok(true, 'post-callback');
818 | // start();
819 | }
820 | };
821 |
822 | // expect(0);
823 | // multiple binds
824 | $('#form9').ajaxForm(opts).submit(function() {
825 | return false;
826 | });
827 | $('#form9').ajaxFormUnbind(opts);
828 | $('#submitForm9')[0].click();
829 |
830 | // setTimeout(start, 500);
831 | });
832 |
833 | it('naked hash', function() {
834 | $('#actionTest1').ajaxSubmit({
835 | beforeSerialize: function($f, opts) {
836 | assert.ok(true, 'url=' + opts.url);
837 | }
838 | });
839 | assert.ok(true, 'ajaxSubmit passed');
840 | });
841 | it('hash only', function() {
842 | $('#actionTest2').ajaxSubmit({
843 | beforeSerialize: function($f, opts) {
844 | assert.ok(true, 'url=' + opts.url);
845 | }
846 | });
847 | assert.ok(true, 'ajaxSubmit passed');
848 | });
849 | it('empty action', function() {
850 | $('#actionTest3').ajaxSubmit({
851 | beforeSerialize: function($f, opts) {
852 | assert.ok(true, 'url=' + opts.url);
853 | }
854 | });
855 | assert.ok(true, 'ajaxSubmit passed');
856 | });
857 | it('missing action', function() {
858 | $('#actionTest4').ajaxSubmit({
859 | beforeSerialize: function($f, opts) {
860 | assert.ok(true, 'url=' + opts.url);
861 | }
862 | });
863 | assert.ok(true, 'ajaxSubmit passed');
864 | });
865 |
866 | it('success callback params', function() {
867 | var $testForm;
868 |
869 | $('#targetDiv').empty();
870 | // stop();
871 |
872 | if (/^1\.3/.test($.fn.jquery)) {
873 | // expect(3);
874 | $testForm = $('#form3').ajaxSubmit({
875 | success: function(data, status, $form) { // jQuery 1.4+ signature
876 | assert.ok(true, 'success callback invoked');
877 | assert.ok(status === 'success', 'status === success');
878 | assert.ok($form === $testForm, '$form param is valid');
879 | // start();
880 | }
881 | });
882 |
883 | } else { // if (/^1\.4/.test($.fn.jquery)) {
884 | // expect(6);
885 | $testForm = $('#form3').ajaxSubmit({
886 | success: function(data, status, xhr, $form) { // jQuery 1.4+ signature
887 | assert.ok(true, 'success callback invoked');
888 | assert.ok(status === 'success', 'status === success');
889 | assert.ok(true, 'third arg: ' + typeof xhr != undefined);
890 | assert.ok(!!xhr != false, 'xhr != false');
891 | assert.ok(xhr.status, 'xhr.status == ' + xhr.status);
892 | assert.ok($form === $testForm, '$form param is valid');
893 | // start();
894 | }
895 | });
896 | }
897 | });
898 |
899 | it('forceSync', function() {
900 | $('#targetDiv').empty();
901 | // stop();
902 |
903 | // expect(2);
904 | var $testForm = $('#form3').ajaxSubmit({
905 | forceSync: true,
906 | success: function(data, status, $form) { // jQuery 1.4+ signature
907 | assert.ok(true, 'success callback invoked');
908 | assert.ok(status === 'success', 'status === success');
909 | // start();
910 | }
911 | });
912 | });
913 |
914 | });
915 |
--------------------------------------------------------------------------------