jquery - 根据另一个选择框中的选择填充一个选择框 - JQuery?

标签 jquery

我正在尝试根据第一个选择框中所做的选择来填充一个选择框。我在网上查找过,发现了很多有关硬编码选项的有用信息,但我需要来自查询的选项(例如 Coldfusion 中的 cfquery)。我知道 cfquery 是服务器端的,所以我不能将它包含在我的 jquery 中,但是还有其他选择吗?

我使用了以下示例:

HTML:

<select id="counties">
    <option> </option>
    <option> somerset </option>
    <option> hertfordshire </option>
</select>

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

JS:

var countyTowns = [
    ["Bath", "Bristol"],
    ["Letchworth", "Hitchin"]
];

$("#counties").change(function() {
    var county = this.selectedIndex - 1;
    $("#towns").empty();
    if (county === -1) {
        $("#towns").attr("disabled", true);
    } else {
        var towns = countyTowns[county];
        for (var i = 0; i < towns.length; i++) {
            $("#towns").append($("<option></option>").text(towns[i]));
        }
        $("#towns").attr("disabled", false);
    }
});

我需要的是城镇是动态的,并且能够从数据库中读取。

非常感谢任何帮助!

谢谢

最佳答案

正如您提到的:城镇是动态的,并且能够从数据库读取,您可以通过使用 ajax 传递动态数据来获取动态数据。

Ajax 从 Controller 获取来读取从基于数据库的国家/地区用户选择的内容:

 <script>
        $(document).ready(function () {
           $("#counties").on('change', function () {
                var selectedItem = $(this).val();
                var ddlStates = $("#towns"); // will be update after success ajax call
                var statesProgress = $("#states-loading-progress");
                statesProgress.show();
                $.ajax({     // get states/towns from db from controller
                    cache: false,
                    type: "GET",
                    url: "@(Url.RouteUrl("GetStatesByCountryId"))", 

                    data: { "countryId": selectedItem, "addSelectStateItem": "true" },
                    success: function (data) { 
                        ddlStates.html('');
                        $.each(data, function (id, option) {
                            ddlStates.append($('<option></option>').val(option.id).html(option.name)); 
                        });  // populating result 
                        statesProgress.hide(); // hide loader
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert('Failed to retrieve states.');
                        statesProgress.hide();
                    }
                });
            });
        });
    </script>

在 Controller 上:

  public virtual IActionResult GetStatesByCountryId(string countryId, bool addSelectStateItem)
    {
        var model = _countryModelFactory.GetStatesByCountryId(countryId, addSelectStateItem);
        return Json(model);
    }

然后在数据访问层 _countryModelFactory.GetStatesByCountryId()我根据用户选择的国家/城镇从数据库获取城镇/州

更新: 这是我从数据库(动态国家/城镇)检索州/城镇并将它们填充到代码中的selectbox的方式。

关于jquery - 根据另一个选择框中的选择填充一个选择框 - JQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5861090/

相关文章:

javascript - JQuery 不工作,因为 Mootools 已经加载到 joomla

javascript - jQuery 与 MVC 中关键字 "this"的含义

jquery - 滑动登录论坛的烦恼

javascript - 获取单击按钮的父元素内的值

窗口加载时的 JavaScript

javascript - 如何捕获复选框输入

jquery - CSS 右过渡问题

javascript - 使用 jQuery 的选项卡式内容 : Show all tabs at once?

javascript - 使用 AJAX 将 cookie session 数据存储到 Jquery(不使用 PHP)

javascript - Jquery Click 事件仅触发一次