欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jquery Ajax 多级联动插件

程序员文章站 2022-06-11 22:36:37
...
#### js 

$('select.ajax-linkage').on('change', function () {
        var $this = $(this),
            $next_node = $($this.data('nextnode')),
            $selectedOpt = $this.find('option:selected'),
            url = $this.data('url'),
            key = $this.data('key') || 'id',
            valueKey = $this.data('valuekey') || 'name';
        if ($selectedOpt.length < 1) {
            $selectedOpt = $('<option value="">请选择</option>')
        }
        var ajax_data = $selectedOpt.data();
        ajax_data['ajax_value'] = $this.val();
        $.ajax({
            url: url,
            type: 'get',
            data: ajax_data,
            dataType: 'json',
            success: function (data) {
                console.log('select.ajax-linkage');
                var options_str = '<option value="">请选择</option>';
                if (data.data) {
                    var displayKeyStr = $this.data('displaykeys') || '',
                        displayKeys = displayKeyStr.split(',');
                    $.each(data.data, function (i, obj) {
                        var value = obj[key] || '';
                        options_str += '<option value="' + value + '" ';
                        var attrs = displayKeys.map(function (k) {
                            return "data-" + k + '="' + obj[k] + '"';
                        });
                        options_str += attrs.join(' ');
                        options_str += '>' + obj[valueKey] + '</option>';
                    });
                    $next_node.html(options_str);
                    $next_node.trigger("change");
                }
            }
        });
    });

 

    <select class="form-control input-inline input-small ajax-linkage" data-url=""
            data-nextnode="#base_l2" data-key="id" data-valuekey="name" data-displaykeys='no,size' id="base_l1" name="base_l1">
      <option value="">请选择</option>... 
    </select>
    <select class="form-control input-inline input-small ajax-linkage" data-url=""
            data-nextnode="#base_l3" data-key="id" data-valuekey="name" data-displaykeys='no' id="base_l2" name="base_l2">
      <option value="">请选择</option>
    </select>
    <select class="form-control input-inline input-small" id="base_l3" name="base_l3">
      <option value="">请选择</option>
    </select>

 

 

 

相关标签: jquery 多级联动