使用jquery Ajax异步刷新 下拉框

一个下拉框

<label>产品类型:</label>
        <select id="protype" name="protype" onchange="getNameList()">
            <option value="">--请选择--</option>
        </select>

响应上一个下拉框的结果

的另一个下拉框

<label>产品名称:</label>
        <select id="proname" name="proname">
            <option value="">--请选择--</option>
        </select>

引入jquery包

<script src="~/Scripts/jquery-1.10.2.min.js"></script>

异步刷新代码

function getNameList() {
        var id = $(‘#protype‘).val();
        if (id === "") {
            $(‘#proname‘).html(‘‘);
            var options = ‘‘;
            options += ‘<option value="">--请选择--</option>‘;
            $(‘#proname‘).append(options);
            return;
        }
        var obj = { id: id };
        AjaxCall(‘/Home/GetProList‘, JSON.stringify(obj), ‘POST‘).done(function (response) {
            if (response.length > 0) {
                $(‘#proname‘).html(‘‘);
                var options = ‘‘;
                options += ‘<option value="">--请选择--</option>‘;
                for (var i = 0; i < response.length; i++) {
                    options += ‘<option value="‘ + response[i].Value + ‘">‘ + response[i].Text + ‘</option>‘;
                }
                $(‘#proname‘).append(options);

            }
        }).fail(function (error) {
            $(‘#proname‘).html(‘‘);
            var options = ‘‘;
            options += ‘<option value="">--请选择--</option>‘;
            $(‘#proname‘).append(options);
        });
    }
    function AjaxCall(url, data, type) {
        return $.ajax({
            url: url,
            type: type ? type : ‘GET‘,
            data: data,
            contentType: ‘application/json‘
        });

后台响应代码

        /// <summary>
        /// 用于处理异步刷新下拉框
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetProList(string id)
        {
            List<SelectListItem> elems = null;
            if (string.IsNullOrEmpty(id))
                elems = null;
            else
            {
                string filePath = Server.MapPath("/App_Data/SimpleData.xml");
                string filter = string.Format("/DD/DItems[@DValue=‘{0}‘]", id);
                elems = GetListByXpath(filePath, filter);
            }
            return Json(elems, JsonRequestBehavior.AllowGet);
        }

总结 :

重点在于使用ajax异步post成功之后,处理服务器返回来的数据

原文地址:https://www.cnblogs.com/c-supreme/p/8687437.html

时间: 2024-08-02 05:51:28

使用jquery Ajax异步刷新 下拉框的相关文章

jQuery Ajax实现下拉框无刷新联动

HTML代码: @{ Layout = null; } @using DAL; @using System.Data; @{ AreaDal areaDal = new AreaDal(); string areaId = ViewBag.areaId; DataRow drArea = areaDal.GetArea(areaId); string countyId = drArea == null ? "-1" : drArea["countyId"].ToSt

jQuery之双下拉框

双下拉框要实现的效果,实际上就是左边下拉选择框里的内容,可以添加到右边,而右边同理.写了个简单的例子,来说明一下. 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 <!DOCTYPEhtml> <html> <head> <title>jq

jQuery操作select下拉框的text值和value值的方法

1.jquery获取当前选中select的text值 var checkText=$("#slc1").find("option:selected").text(); 2.jquery获取当前选中select的value值 var checkValue=$("#slc1").val(); 3.jquery获取当前选中select的索引值 var index=$("#slc1 ").get(0).selectedIndex; 4

JS/JQuery操作select下拉框

一.js 操作select 下拉框 var selObj = 下拉框对象 1. 移除所有项:selObj.options.length = 0; 2. 移除下拉框中的一项:selObj.options.remove(index); “index”为下拉框选项的索引值,若0索引项移出(自上而下),那么1索引项的索引会变为0,后面的索引依次向前推进 也可利用循环,移除所有项: var length = selObj.options.length; for(var i=length-1;i>=0;i-

layui与jquery冲突导致下拉框无法显示的解决方法

1.背景: 在使用 layui 框架写 jsp 的时候,使用 ajax 传递数据来刷新表单,发现使用 ajax 引用外部的jquery 和 layui 自带的jquery中,可能是 $ 导致select 下拉框中的option 没法在layui中正常使用(即数据传过去了,但是点击下拉框没有任何数据) 2.解决 查了一下网上的解决方法.有个解决方法是 在layui.js之前引用其他jquery .但是发现 并没有什么卵用. 后来发现,ajax传递数据在加载页面之后,导致了导入的option没有在第

Jquery动态设置下拉框selected --(2018 08/12-08/26周总结)

1.Jquery动态根据内容设置下拉框selected 需求就是根据下拉框的值动态的设置为selected,本以为很简单,网上一大推的方法,挨着尝试了之后却发现没有一个是有用的.网上的做法如下: <select id="selectID "> <option>选择A</option> <option>选择B</option> <option>选择C</option> </select> //

jquery Combo Select 下拉框可选可输入插件

Combo Select 是一款友好的 jQuery 下拉框插件,在 PC 浏览器上它能模拟一个简单漂亮的下拉框,在 iPad 等移动设备上又能回退到原生样式.Combo Select 能够对选项进行检索过滤,同时支持键盘控制. 支持浏览器: Safari, Chrome, Firefox, Opera iOS, Android, IE Mobile Internet Explorer IE 8 and above 开源地址:http://www.oschina.net/p/combo-sele

Jquery学习笔记:利用jquery获取select下拉框的值

jquery不是特别熟练,每次使用不常用的就要百度,特地记录下来. 我的下拉框是: <div class="form-group"> <select class="form-control" id="iv_level"> <option value="">店员</option> <option value="">店长</option>

jquery操作select下拉框的多种方法(选中,取值,赋值等)

jQuery获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发2. var checkText=$("#select_id").find("option:selected").text();  //获取Select选择的Text3. var checkValue=$("#select