如何将自动填充结果列为链接?

时间:2013-03-14 14:19:19

标签: javascript asp.net ajax

我是Ajax的新手。我需要找到如何将自动完成结果列为链接。当用户单击结果时应打开该链接。我可以列出相关的结果,但我找不到如何添加链接。它应该作为html标记添加到脚本中的某个位置。请给我一些线索如何添加html链接

这是我的剧本:

<script type="text/javascript">
    $(document).ready(function () {
        SearchText();
    });
    function SearchText() {
        $(".auto").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default.aspx/GetAutoCompleteData",
                    data: "{'question':'" + document.getElementById('txtQuestion').value + "'}",
                    dataType: "json",
                    success: function (data) {
                        response(data.d);
                    },
                    error: function (result) {
                        alert("Error Occurred");
                    }
                });
            }
        });
    }
</script>

以下是连接到db并返回相关结果的方法:

[WebMethod]
public static List<string> GetAutoCompleteData(string question)
{
    List<string> result = new List<string>();
    using (SqlConnection conn = new SqlConnection("Data Source=xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx+"))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT Questions,Link FROM DigiQA WHERE Questions LIKE '%'+@quest+'%'", conn))
        {
            conn.Open();
            cmd.Parameters.AddWithValue("@quest", question);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                result.Add(dr["Questions"].ToString());
            }
            return result;
        }
    }   
}

1 个答案:

答案 0 :(得分:1)

尝试这样的事情

success: function(data) {
    response($jQuery.map(data, function(item) {
        return {
           label: '<a href="yoururl">' + item + '</a>'),
           value: item
        }
    }))
}