I’m trying to implement chained ajax selects from this jQuery plugin: http://www.appelsiini.net/projects/chained
I’m using the remote method.
I have a problem where my select boxes show the “default” (blank) value last in the select as seen here:

This is a problem because I have 5 chains, so when the blank value isn’t selected by default, it does 5 sequential lookups. It also isn’t working like it shows on the demo page where the default value is selected by default instead of a real value.
A JSON request from my server returns this:
{"":"","1":"Test #1","2":"Test #2"}
So as you can see, it’s not the order that my JSON is returning.
This is my HTML:
<label class="control-label" for="branch">Branch</label>
<select id="branch" name="branch">
<option value=""></option>
<option value="1">Foo</option>
<option value="2">Bar</option>
</select>
<label class="control-label" for="facility">Facility</label>
<select id="facility" name="facility">
<option value=""></option>
</select>
<script src="/js/chained.js"></script>
<script>
$(document).ready(function() {
$("#facility").remoteChained("#branch", "/src/record.json.php");
} );
</script>
So now I’m lost and since I can’t read/write JavaScript very well, I’m hoping someone can see the issue and help me out.
It happens because,
Chrome and probably Opera sort object properties automatically
So your JSON also is sorted in
forloop.DEMO
Solution 1:
If you change your index as string that will not be sorted.
DEMO
Solution 2:
Changing plug-in
Here is the edited plugin code that works with array,
To work with this updated plugin, you should use this JSON format,
If you want to set a default selected item (for example “Test #1” is selected), so you can do that like this,
How can I use this edited plugin?
Just clear all the code in
/js/chained.jsthan paste the new one.