2 * Chained Selects for jQuery
3 * Copyright (C) 2008 Ziadin Givan www.CodeAssembly.com
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/
19 * settings = { usePost : true, before:function() {}, after: function() {}, default: null, parameters : { parameter1 : 'value1', parameter2 : 'value2'} }
20 * if usePost is true, then the form will use POST to pass the parameters to the target, otherwise will use GET
21 * "before" function is called before the ajax request and "after" function is called after the ajax request.
22 * If defaultValue is not null then the specified option will be selected.
23 * You can specify additional parameters to be sent to the the server in settings.parameters.
28 jQuery
.fn
.chainSelect = function( target
, url
, settings
)
30 return this.each( function()
32 $(this).change( function( )
40 parameters
: {'_id' : $(this).attr('id'), '_name' : $(this).attr('name')}
43 settings
.parameters
._value
= $(this).val();
45 if (settings
.before
!= null)
47 settings
.before( target
);
50 ajaxCallback = function(data
, textStatus
)
52 $(target
).html("");//clear old options
53 data
= eval(data
);//get json array
55 for (i
= 0; i
< data
.length
; i
++) {
56 $(target
).get(0).add(new Option(data
[i
].name
, data
[i
].value
), document
.all
? i
: null);
59 $(target
).get(0).add(new Option('- select a country -', ''), document
.all
? i
: null);
62 if (settings
.defaultValue
!= null)
64 $(target
).val(settings
.defaultValue
);//select default value
67 $("option:first", target
).attr( "selected", "selected" );//select first option
70 if (settings
.after
!= null)
72 settings
.after(target
);
75 $(target
).change();//call next chain
78 if (settings
.usePost
== true)
80 $.post( url
, settings
.parameters
, ajaxCallback
);
83 $.get( url
, settings
.parameters
, ajaxCallback
);