【问题标题】:asp.net webservice jquery textbox autocompleteasp.net webservice jquery 文本框自动完成
【发布时间】:2017-08-26 20:57:56
【问题描述】:

我正在尝试实现 jquery - 文本框自动完成。为此设置一个 Web 服务。

    [WebMethod]


    public List<Condition> GetCondition(string term)
    {


        List<Condition> listCondition = new List<Condition>();

        string cs = ConfigurationManager.ConnectionStrings["db5"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("spSelCondition", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter()
            {
                ParameterName = "@term",
                Value = term
            });
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                listCondition.Add(new Condition { ConditionID = rdr["ConditionID"].ToString(), ConditionName = rdr["ConditionName"].ToString() });
            }
            return listCondition;
        }
    }

    public class Condition
    {
        public string ConditionName { get; set; }
        public string ConditionID { get; set; }
    }

WebService 工作得非常好。要使用我编写的 jquery 自动完成此 javascript 填充文本框。

<script type="text/javascript">
            $(document).ready(function () {

                $('#txtCondition').autocomplete({
                    source: function (request, response) {
                        $.ajax({
                            url: 'ibs_ws.asmx/GetCondition',
                            method: 'post',
                            contentType: 'application/json;charset=utf-8',
                            data: JSON.stringify({ term: request.term }),
                            dataType: 'json',
                            success: function (data) {
                                response(data.d);


                            },
                            error: function (err) {
                                alert(err);
                            }
                        });
                    }

            });
        })</script>

当我访问 web 服务时,它以 @term 作为参数并以 xml 格式返回带有 ConditionID 和 ConditionName 的结果。但是在网络表单上,当我在文本框中输入任何内容时,它会以 [object object] 发出警报。

可能是什么问题。

-- 感谢和问候

【问题讨论】:

    标签: c# jquery asp.net web-services


    【解决方案1】:

    也许它会帮助你。试试这个

    我认为您需要使用 $.map(data.d, function (value, key) {})。这是需要更新成功代码的示例,如下所示,然后您可以在对象中获取值。

    success: function (data) {
                            response($.map(data.d, function (value, key) {
                                return {
                                    ConID: value.ConditionID,
                                    ConNAme: value.ConditionName,
                                }
                            }))
                        },
    

    正如您所说,您的服务返回条件 ID 和名称,所以我使用了这些变量,您可以完全匹配它返回的内容。

    【讨论】:

      猜你喜欢
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 2014-07-26
      • 2011-06-06
      • 1970-01-01
      • 2014-12-15
      相关资源
      最近更新 更多