Cdore
Cdore

Reputation: 115

One drop down list contents depending on another

I am attempting to create two drop down lists: drop down list A lists a country. And drop down list B lists a city. List B is initially empty while List A is populated by a country. A user can choose a country, causing List B to automatically list the cities that are in List A. These will naturally come from a database, so this is not the target of my question. My question is how do I bind List B to be dependent on List A. I've spent a good couple of hours researching the answer and trying out various jquery and javascript methods. I'm stuck at trying to get List B to respond to List A by using the change method of List A, but thus far, nothing seems to be working, nor am I able to trigger a response from List B in terms of adding test values.

How do I do this?

Upvotes: 1

Views: 1696

Answers (4)

maxum
maxum

Reputation: 2915

Hard to answer with out a database scheme but here goes.

Assume your DB looks like

    Countrys
 +--------------+
 | New Zealand  | 
 +--------------+
 | Australia    |
 +--------------+
 | India        |
 +--------------+

 Towns

    Country       Town
 +--------------+---------------+
 | NZL          | Auckland      |
 +--------------+---------------+
 | NZL          | Wellington    |
 +--------------+---------------+
 | NZL          | Christchurch  |
 +--------------+---------------+
 | AU           | Sydney        |
 +--------------+---------------+
 | AU           | Wagawaga      |
 +--------------+---------------+
 | AU           | Brisbane      |
 +--------------+---------------+
 | IN           | Mumbai        |
 +--------------+---------------+

......etc etc

First Select

  <select onchange ="town()" id="country">
  <option value = "NZ">New Zealand</option> 
  <option value = "AU">Australia</option>
  <option value = "IN">India</option>
  </select>

second one

  <select id="towns"></select>

Javascript

  function town()
  {
  var town = $('#country').val()
  $.ajax({
        type: "POST",
        url: "http://yourserver/rpc.php",
        data: { method: 'get_towns'},
        dataType: "json",
        timeout: 10000, // in milliseconds
        success: function(data) {
            $("#towns".html(data.towns)
            },
        error: function(request, status, err) {
            if(status == "timeout") {
                $.ui.hideMask();
               alert('This is taking too long. You could try again now, or wait and try again later.');
    }
    }
    }); 

}

return towns in json from your db

Upvotes: 0

Vaibhav Jain
Vaibhav Jain

Reputation: 3755

Here is the working fiddle: Relative Drop-down

$(function() {
var cities = {
    'INDIA': ['Delhi', 'Mumbai', 'Bangalore', 'Ahmedabad'],
    'USA': ['London', 'Los Angeles', 'Austin', 'New York']
};
var hashFunc = function(country, city){
    return country + "." + city;
};
//The form
var form = new Backbone.Form({
    schema: {
        country: { type: 'Select', options: ['INDIA', 'USA'] },
        city: { type: 'Select', options: cities.INDIA},
    }
}).render();
form.on('country:change', function(form, countryEditor) {
    var country = countryEditor.getValue(),
        newOptions = cities[country];
    form.fields.city.editor.setOptions(newOptions); 
});  
//Add it to the page
$('body').append(form.el);
});

Upvotes: 2

PSR
PSR

Reputation: 40338

$('#select1').change(function(){
callAjax(this.value);
});

function callAjax(value1)
{

//here write the code for ajax

}

Upvotes: 0

Janak
Janak

Reputation: 5052

When any option is selected, Depending on its value, you can create dynamic options to another select.

$('#A').change(function() {
  if($('#A:selected').val() == "India"){

     $("<option></option>", 
        {value: "Surat", text: "Surat"})
       .appendTo('#B');
  } 
});

Upvotes: 0

Related Questions