【问题标题】:Dependent / Cascading Dropdown依赖/级联下拉
【发布时间】:2014-02-19 15:50:46
【问题描述】:

我正在使用级联下拉 jquery 插件。 (https://github.com/dnasir/jquery-cascading-dropdown)

我有两个下拉菜单。 “客户”和“网站”。

根据您选择的客户端,站点列表应适当减少,以仅显示所选客户端的站点。我设置了两个 php 脚本 returnClientList.php 和 returnSiteList.php 成功返回带有标签/值对的 json 数组。

我的问题是选择客户端后无法减少站点列表。该事件成功触发,但我只得到完整列表。正如您将看到的,代码使用 getJSON 请求,我从手册中知道该请求发送 HTTP GET。查看 chrome 的 network 面板显示实际上没有发送 GET 值。

希望一些显而易见的事情,但我是 jquery 的新手,因此感谢您的帮助。

我的代码:

JS

$('#edit-shift').cascadingDropdown({
selectBoxes: [
    {
      selector: '.clients',
      source: function(request, response) {
      $.getJSON('returnClientList.php', request, function(data) {
                  var selectOnlyOption = data.length <= 1;
                  response($.map(data, function(item, index) {
                      return {
                          label: item.label,
                          value: item.value,
                          selected: selectOnlyOption // Select if only option
                      };
                  }));
              });
          }
    },
    {
        selector: '.sites',
        requires: ['.clients'],
        source: function(request, response) {
            $.getJSON('returnSiteList.php', request, function(data) {
                var selectOnlyOption = data.length <= 1;
                response($.map(data, function(item, index) {
                    return {
                        label: item.label,
                        value: item.value,
                        selected: selectOnlyOption // Select if only option
                    };
                }));
            });

        }
    },
    {
        onChange: function(event, value, requiredValues){}
    }
]
});

PHP

//this script returns a json array for use in jquery autocomplete fields for site     lists...
header('Content-type: application/json');
require("connect.php");

$client_id = $_GET['?'];

 //do the query for sites that are active
$sql =  "SELECT * FROM site WHERE active=1 AND client_id='$client_id' ORDER BY site_name ASC";
$result = mysql_query($sql) or die('Error: ' . mysql_error());

//loop the results and create php array
while($row = mysql_fetch_array($result)){
    $arr[] = array('label' => $row['site_name'], 'value' => $row['id']);
}

echo json_encode($arr);

【问题讨论】:

    标签: javascript php jquery json get


    【解决方案1】:

    最终为此编写了我自己的解决方案并废弃了该插件。享受。

    //dynamically returns the sites once the user chooses a client - edit/add shift form
      $('.client-id').change(function () {
        var selectedClient = $(this).val();
        if (selectedClient != null && selectedClient != '') {
    
            $.getJSON('returnSiteList.php', { id: selectedClient }, 
                        function (Sites) {
                          var siteSelect = $('.site-id');
                          siteSelect.empty();
                          $.each(Sites, function (index, site) {
                          siteSelect.append($('<option/>', {
                          value: site.value,
                          text: site.label
                    }));
                });
            });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2016-08-13
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      相关资源
      最近更新 更多