Auto-populate multiple select boxes
To follow on from Auto-populating Select Boxes using jQuery & AJAX, I've had more than a few requests for how to do this for multiple select boxes.
In response, I've written a jQuery plugin and have included a simple example of three select boxes populating each other driven by a MySQL database.
Download
- Download the selectChain plugin
- View the demo PHP script source
- View the demo SQL source
- Demonstration
Prerequisites
- jQuery (this has been tested with 1.1.1 and 1.2 - so it should be fine).
- Basic knowledge of JSON
- Access to the server side for PHP and MySQL if you want the selects driven by a database.
Disclaimer
I have to admit I wrote the plugin pretty quickly, so it might not have all the bells and whistles you might want it to have - but it definitely does the job.
Also, the demo I've provided does not degrade if JavaScript is turned off. I advocate that you practise accessible JavaScript. This example is just to show the plugin working. Remember to make it work via the server-side too!
Demonstration
This demonstration uses three select boxes, the first (element category) drives the next (elements) which drives the next (attributes).
You should keep in mind this demo was written quickly, and I would never normally use the same name attribute on a select box, because when it comes to actually submitting the form, it would be a mess of values.
See the multiple-select population in action
Usages / Config
$('#categorySelect').selectChain({
target: $('#childCategorySelect'),
url: 'update-options.php'
});
Required
- target: jQuery object or HTML element
- url: string to Ajax request
Optional
- key: key of the key/value pair if you're returning an array of objects. Defaults to 'id'.
- value: value of the key/value pair if you're returning an array of objects. Defaults to 'label'.
- data: additional data values to send in the request (can be a string or object)
- type: Ajax request type, i.e. post or get
What next?
Here's a list of small bits that I think could be added to improve this plugin, but should be simple enough for anyone to write:
- Caching Ajax results.
- Ability to send the data (posted or get) based on a dynamic criteria - i.e. perhaps there's another static select box.
- Using meta data in the classes to link the targets to the parents - but this might make for sloppy markup.
Feel free to add any suggestions or improvements.
You should follow me on Twitter here I'll tweet about JavaScript, HTML 5 and other such gems (amongst usual tweet-splurges)
Thanks for this plugin ! it saves me a lot of time !
How can I assing the default 'elementSelect' ?
Best regards and thank you for this great plugin.
[...] Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect Auto-populate multiple select boxes. Choose Plugin (Select [...]
hi, nice tool!
it must be a very stupid question but I'm trying to apply this tool to postgreSQL, but I always get an empty [] value. It means that the array $json[] is not filled in the while statement. But the dbase connection and the query is correct ( I can check applying a echo "data: ".$row[0].""; and all the data is displayed).
Thanks!
[...] Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect Auto-populate multiple select boxes. Choose Plugin (Select [...]
@pere - first things first, I would ensure the query is returning results. I normally do this by:
That way I know for sure the values being passed in to the query (and I'll run that directly in to the database).
The rest of the code looks fine, so I would start there.
The js dont work with ie due to a bug of ie
http://support.microsoft.com/kb/276228/fr
(select chain bieng the new one )
Solution
well hum .... this should be more readable :
http://corpo.julbox.net/jquery_select.js
[...] Auto-populate multiple select boxesLanuch Demo/site [...]
@Jul - thanks for the heads up on the IE bug. I've patched the plugin to use your suggested code (note that your version hard codes 'entity' as an id, break the chainability of different selects - it's fine in the plugin code though).
Cheers.
Remy,
I solved the question like this; thanks.
Hey, it still automaticly chooses first node in box and gets its children. And it would be nice to be able to select node by id
[...] Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect Auto-populate multiple select boxes. Choose Plugin (Select [...]
[...] Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect Auto-populate multiple select boxes. Choose Plugin (Select [...]
[...] Auto-populate multiple select boxes. [...]
[...] Select List.Multiple Selects.Select box manipulation.Select Combo Plugin.jQuery - LinkedSelectAuto-populate multiple select boxes.Choose Plugin (Select Replacement). 表单基本、输入框、选择框等(Form Basics, Input [...]
This is great, I'll implement it now and give the URL, so people have another example
hi,
it's very nice, but I need following:
select box 1
select box 2
select box 3
result
it should be like a filter for result...
please help me, I have no idea
[...] Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect Auto-populate multiple select boxes. Choose Plugin (Select [...]
[...] Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect Auto-populate multiple select boxes. Choose Plugin (Select [...]