javascript - 如何创建多选以显示 Django 中每个选定内容的详细信息?

标签 javascript jquery python django

Description Image Demo

我想创建一个如图所示的小演示,如果我单击左栏中的项目来选择国家/地区,右栏将显示我选择的左栏中的国家/地区的所有城市。我怎样才能做到这一点?

最佳答案

我写了一个小样本,希望对您有帮助:

<html>
   <head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <style>
         select {
         min-width: 100px;
         float: left;
         margin-right: 20px;
         }
      </style>
   </head>
   <body>
      <select id="countrySelect" size="10"></select>
      <select id="citySelect"  size="10"></select>
      <script>
         var countries = [{name: "USA", id:1}, {name: "Canada", id:2}];             
         var cities = [{name: "New York", id:1, countryId:1}, {name: "Boston", id:2, countryId:1}, {name: "Ottawa", id:3, countryId:2}, {name: "Toronto", id:4, countryId:2}];

         var $countrySelect = $('#countrySelect');
         var $citySelect = $('#citySelect'), countrySelectHtml = '';
         for (var i=0; i<countries.length; i++) $countrySelect.append($('<option>').attr('value', countries[i].id).text(countries[i].name));

         $countrySelect.change(function () {
            var countryId = $countrySelect.val();
            $citySelect.html('');
            for (var i=0; i<cities.length; i++) 
                if (cities[i].countryId == countryId) $citySelect.append($('<option />').attr('value', cities[i].id).text(cities[i].name));
         });

         $citySelect.change(function () {
            alert('Selected city: ' + $(this).find('option:selected').text() + ' (id=' + $citySelect.val() + ')');
         });

      </script>
</html>

关于javascript - 如何创建多选以显示 Django 中每个选定内容的详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37041823/

相关文章:

javascript - 将html表格数据放入google电子表格

jquery - 使用 JQuery 从中心动画增长

python - 如何使用python在h1标签之后在现有html文件中添加新的div标签

python - 具有多个 for 子句的列表理解的 Map/reduce 等价物

javascript - 从 JSON 获取值到 HTML

javascript - 饼图 3d json 系列

javascript - Controller 中未定义的 Ng-model

javascript - 导入后导出的模块未定义

jquery - jquery 和 codeigniter 中的 if else 条件检查和取消选中复选框

python - 为什么我不能导入tensorflow