2

我正在尝试通过 jquery 自动完成组合框的 jquery/json ajax 方法从数据表中加载数据,但它不起作用这是我的代码:

我的脚本:

<script type='text/javascript'>//<![CDATA[ 
 $(function() {
    // filter
    $.widget("ui.combobox", {
        _create: function() {
            var self = this,
                select = this.element.hide(),
                selected = select.children(":selected"),
                value = selected.val() ? selected.text() : "";
            var input = this.input = $("<input>").insertAfter(select).val(value).autocomplete({
                delay: 0,
                minLength: 0,
                source: function(request, response) {
                    $.ajax({
                        url: "InventoryDetails_new.aspx/BindHostDetails('SIU')",
                        type: "POST",
                        dataType: "jsonp",
                        data: {
                            featureClass: "P",
                            style: "full",
                            maxRows: 12,
                            name_startsWith: request.term
                        },
                        success: function(data) {
                            response($.map(data.geonames, function(item) {
                                return {
                                    label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                    value: item.name
                                }
                            }));
                        }
                    })
                },
                select: function(event, ui) {

                },

            }).addClass("ui-widget ui-widget-content ui-corner-left");

            input.data("autocomplete")._renderItem = function(ul, item) {
                return $("<li></li>").data("item.autocomplete", item).append("<a>" + item.label + "</a>").appendTo(ul);
            };

            this.button = $("<button type='button'>&nbsp;</button>").attr("tabIndex", -1).attr("title", "Show All Items").insertAfter(input).button({
                icons: {
                    primary: "ui-icon-triangle-1-s"
                },
                text: false
            }).removeClass("ui-corner-all").addClass("ui-corner-right ui-button-icon").click(function() {
                // close if already visible
                if (input.autocomplete("widget").is(":visible")) {
                    input.autocomplete("close");
                    return;
                }

                // work around a bug (likely same cause as #5265)
                $(this).blur();

                // pass empty string as value to search for, displaying all results
                input.autocomplete("search", "");
                input.focus();
            });
        },

        destroy: function() {
            this.input.remove();
            this.button.remove();
            this.element.show();
            $.Widget.prototype.destroy.call(this);
        }
    });

    $("#cbCity").combobox({
        source: "InventoryDetails_new.aspx/BindHostDetails('SIU')",
        dataType: "jsonp",
        minLength: 2,
        select: function(event, ui) {
            log(ui.item ? "Selected: " + ui.item.value + " aka " + ui.item.id : "Nothing selected, input was " + this.value);
        }

    });
}); //]]> 
</script>

 [WebMethod]
    public ArrayList BindHostDetails(string strStockCategory)
    {
        string strSql = "";
        if (strStockCategory == "SIU")
        {
            strSql = "select distinct(nvrHostName) from tblInventoryDetails where  intStatus =1  and nvrStockCategory = '" + strStockCategory.ToString() + "'";
        }
        else
        {
            strSql = "select distinct(nvrHostName) from tblInventoryDetails where  intStatus = 0  and nvrStockCategory = '" + strStockCategory.ToString() + "'";
        }
        DataTable objDT = new DataTable();
        objDT = objDataLayer.GetDataStoreInTable(strSql);
        DataSet ds = new DataSet();
        ds.Tables.Add(objDT);
        ArrayList arrlst = new ArrayList();
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            arrlst.Add(row);
        }
        return arrlst;
    }
4

1 回答 1

0

您使用错误的方式调用 webmwthod。它可以帮助你

在 ajax 工具包模式弹出扩展器上回发

于 2012-08-11T09:51:54.090 回答