用AJAX更新DropDownList

用AJAX更新DropDownList

键入客户端名称#NAME1后,我试图从db中的其他表获取该客户端子帐户#SUB在列表中的列表,但我的列表为空。我在做什么错?

html

 <script type="text/javascript">
    var kunnr; var ROLA;
    $(document).ready(function () {

        $('#NAME1').autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "Form2",
                    method: 'POST',
                    data: {
                        term: $('#NAME1').val()
                    },
                    success: function (data) {
                        response(data);
                    }});},
            select: function (event, ui) {
                kunnr = ui.item.kunnr;
            }});});
    $('#NAME1').change; ({
        source: function (request, response) {
            $.ajax({
                type: "GET",
                url: "Form3",
                method: 'POST',
                data: {
                    term: $('#SUB').val(), kunnr: kunnr, ROLA:"Slave"
                },
                success: function (data) {
                    response(data);
                }});}, });


控制者

    [HttpPost]
    public JsonResult Form2(string term)
    {
        return Json(db.KLIENCI.Where(c => (term!=null && c.NAME1.Contains(term))||(term==null)).OrderBy(x=>x.NAME1).Take(10).Select(a => new { label = a.NAME1,kunnr=a.KUNNR }));
    }
    [HttpPost]
    public JsonResult Form3(string term, string kunnr)
    {
        return Json(db.CLI2LOGIN.Where(c => ((term != null && c.LOGIN.Contains(term)) || (term == null))&& c.KUNNR== kunnr && c.ROLA == "Slave").OrderBy(x => x.LOGIN).Select(a => new { label = a.LOGIN }).ToList(),JsonRequestBehavior.AllowGet);}

最佳答案

请在下面找到一个例子:

视图:

<script>
$(document).ready(function () {
    $('.maincat').change(function () {
        updateSubCategoryList($(this).val());
    });

function updateSubCategoryList(catId) {
    $('.subcat').empty();
    $('.subsubcat').empty();
    $.ajax({
        url: '@Url.Action("GetSubCategories")',
        type: 'Get',
        data: { main: catId },
        success: function (response) {
            var subcat = $('.subcat');
            subcat.append('<option value=""></option>');
            if (response != null && response != '') {
                $.each(response, function (index, value) {
                    subcat.append('<option value="' + this.SubCategoryID + '">' + this.SubCategoryDescription + '</option>');
                });
            }
        }
    });
}
</script>


控制器:

        [HttpGet]
        public JsonResult GetSubCategories(int main)
        {
            var i = _jobsservice.GetSubCategories(main);
            return Json(i, JsonRequestBehavior.AllowGet);
        }

关于javascript - 用AJAX更新DropDownList,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56524139/

10-13 01:34