ajax处理select下拉表单

    $('#gameid').change(function() {
        var gameid = $(this).val();
        if (this.value != '') {
            $.ajax({
                url: '/market/task/?act=ajax_get_game_server_list&gameid=' + gameid,
                type: 'get',
                dataType: 'json',
                beforeSend: function() {
                    $('#load_game_server').html("<image src='http://pages.19youxi.me/admin/css/images/loading.gif'>");
                },
                success: function(data) {
                    if (data.errorNum == 0) {
                        var game_server_list = data.game_server_list;
                        $("#serverid").find('option').eq(1).nextAll().remove();
                        if (game_server_list) {
                            for(var i=0; i < game_server_list.length; i++) {
                                var option = "<option value='" + game_server_list[i]['id'] + "'>" + game_server_list[i]['servername'] + "</option"
                                $("#serverid").append(option);
                            }
                        }
                        $("#load_game_server").html("");
                    } else {
                        alert(data.errorMsg);
                    }
                },
                error: function() {
                    alert('未知错误!');
                }
            });
        }
    });
      
返回后处理data还可以用map函数处理:
$(function() {

$(".chzn-select").chosen();
$(".chzn-select-deselect").chosen({allow_single_deselect:true});

$('.chzn-choices input').autocomplete({
   source: function( request, response ) {
      $.ajax({
          url: "/change/name/autocomplete/"+request.term+"/",
          dataType: "json",
          success: function( data ) {
             response( $.map( data, function( item ) {
                $('ul.chzn-results').append('<li class="active-result">' + item.name + '</li>');

          }
       });
    }
});


 
原文地址:https://www.cnblogs.com/Alight/p/3452425.html