简体   繁体   English

一个下拉列表内容取决于另一个

[英]One drop down list contents depending on another

I am attempting to create two drop down lists: drop down list A lists a country. 我正在尝试创建两个下拉列表:下拉列表A列出了一个国家。 And drop down list B lists a city. 下拉列表B列出了一个城市。 List B is initially empty while List A is populated by a country. 列表B最初是空的,而列表A是由一个国家填充的。 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. 用户可以选择一个国家,从而使列表B自动列出列表A中的城市。这些自然会来自数据库,因此这不是我要提出的目标。 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. 我的问题是如何绑定列表B以使其依赖于列表A。我花了很多时间研究答案,并尝试了各种jquery和javascript方法。 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. 我一直试图通过使用列表A的更改方法来使列表B响应列表A,但到目前为止,似乎没有任何效果,就添加测试而言,我也无法触发列表B的响应值。

How do I do this? 我该怎么做呢?

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);
});

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');
  } 
});
$('#select1').change(function(){
callAjax(this.value);
});

function callAjax(value1)
{

//here write the code for ajax

}

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 使用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 从您的数据库中返回json中的城镇

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个下拉列表更改下拉列表值 - Change drop down list values depending on another drop down list 在PHP中将下拉选择列表的内容从一种形式传递到另一种形式 - Pass contents of drop-down select list from one form to another in PHP 根据另一个下拉列表中的所选选项更改下拉列表中的项目 - Change items in a drop-down list depending on the selected option in another drop-down list 根据另一个选定的下拉列表更改下拉列表中的选项 - Change options in a drop-down list depending on another selected drop-down list 如何根据另一个下拉框在下拉框中创建列表? - How do I make a list in a drop down box depending on another drop down box? React Native:我正在动态设置一个下拉菜单的选项,具体取决于另一个下拉列表的值选择。 但是这个选择 - React Native: I'm dynamically setting options of one drop down depending on another drop down value selection. But this.setlection 下拉列表根据内容的宽度变化 - drop down list width change depending on the content Ruby on Rails-下拉列表触发另一个 - Ruby on Rails - Drop down list triggers another one 当在一个下拉列表中选择一个选项时,如何使用javascript在另一个下拉列表中显示其相关值 - when one option selected in one drop down list then how to display its related value in another drop down list using javascript 基于另一个下拉列表启用/禁用下拉列表 - Enable/disable drop down list based on another drop down list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM