【问题标题】:jquery AutoComplete textbox populate value with respect to Seleted value of Dropdownlistjquery AutoComplete 文本框相对于下拉列表的选定值填充值
【发布时间】:2017-12-13 08:16:29
【问题描述】:

我如何填充我的 jQuery 自动完成文本框相对于 asp.net 中数据库中下拉列表的选定值。我想根据下拉列表选择在自动完成中显示值。这是我在下拉列表中选择值时的代码它可以'不调用 web 服务,我不知道我在哪里做错了,如何在下拉 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;
        }

    });
});

【问题讨论】:

  • [id$=txtCode] => 你确定这是一个正确的选择器吗?您是否在控制台和检查网络选项卡中遇到错误以确保请求由目标 URL 解析?
  • 我在控制台中没有任何错误

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


【解决方案1】:

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

$("[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;
        }

        }
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多