jquery AutoComplete文本框根据Dropdownlist的Seleted值填充值

时间:2017-12-13 08:16:30

标签: jquery asp.net webforms jquery-ui-autocomplete

如何在asp.net中从数据库填充关于Dropdownlist的选定值的jQuery自动完成文本框。我想根据下拉列表选择在自动完成中显示值。当我在下拉列表中选择值时,我的代码可以& #39; t调用网络服务我不知道我哪里做错了如何在dropdown selectindexchange事件中调用服务

var ddl = document.getElementById('<%=cmbSourceCode.ClientID %>');
$(function () {

    $("[id$=txtCode]").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '<%=ResolveUrl("xCreate_grn.aspx/GetSourceCode") %>',
                data: "{ 'prefix': '" + request.term + "','code':'"+ddl.SelectedIndex+ "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return {
                            label: item.split('-')[0],
                            val: item.split('-')[1]
                        }
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        minLength: 4,
        focus:function(event,ui){
          event.preventDefault();
          this.value = ui.item.label;
        }

    });
});

1 个答案:

答案 0 :(得分:0)

我解决了我的问题,我错误地传递了选定的下拉值,所以我的函数在我的代码中使用之后现在无法正常工作

$("[id$=txtCode]").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '<%=ResolveUrl("xCreate_grn.aspx/GetSourceCode") %>',
                data: "{ 'prefix': '" + request.term + "','code':'"+ 
                          $('select[id=cmbSourceCode]').val() + "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return {
                            label: item.split('~')[0],
                            val: item.split('~')[1]
                        }
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        minLength: 4,
        focus:function(event,ui){
          event.preventDefault();
          this.value = ui.item.label;
        }

        }
    });
});
相关问题