从数据库中获取单词并将其传递给在浏览器中运行的脚本文件

时间:2015-08-28 03:42:35

标签: javascript jquery arrays autocomplete

    <body>

            <h3 id="apply-to-multiple-textareas">Apply to Multiple Textareas</h3>
            <div class="textarea-wrapper">
                <textarea class="textarea4 form-control" rows="4"></textarea>
            </div>
            <div class="textarea-wrapper">
                <textarea class="textarea4 form-control" rows="4"></textarea>
            </div>



            </body>            

<script type="text/javascript" class="brush: js; script" >

            $('.textarea').textcomplete([
                { // tech companies
                    words: [],
    <%
    Class.forName("com.mysql.jdbc.Driver");
    Connection con4=DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital","root","root");
    String vsql4 ;
    try
    {
        Statement st4=con4.createStatement();
        vsql4="select hi from new1";
        ResultSet Rs4=st4.executeQuery(vsql4);
        while(Rs4.next())
        {
            %>
            words.push("<%= Rs4.getString("hi") %>");
            <%
        }
        %>

                    match: /\b(\w{2,})$/,
                    search: function (term, callback) {
                        callback($.map(this.words, function (word) {
                            return word.indexOf(term) === 0 ? word : null;
                        }));
                    },
                    index: 1,
                    replace: function (word) {
                        return word + ' ';
                    }
                }
            ]);
            </script>

我尝试了这个,但没有得到结果是错误的我在这里我是新的javascript我将自动编译我的textarea它适用于单词:['apple', 'google', 'facebook', 'github'],但当我从db检索它没有得到结果

1 个答案:

答案 0 :(得分:0)

检查一下,它可能对你有帮助

<head id="Head1" runat="server">
<title></title>
<link href="Styles/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="Scripts/jquery-1.9.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript">
           $(document).ready(function () {
$("#txtSearch").autocomplete({
    source: function (request, response) {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "WebService.asmx/GetData",
            data: "{'DName':'" + document.getElementById('txtSearch').value + "'}",
            dataType: "json",
            success: function (data) {
                response(data.d);
            },
            error: function (result) {
                alert("Error......");
            }
        });
    }
});
});
 </script>
</head>

public class WebService : System.Web.Services.WebService
{
[WebMethod]
public List<string> GetData(string DName)
{
    List<string> result = new List<string>();
    using (SqlConnection con = new SqlConnection("Data Source=SYSTEM-30;database=DB_Test_Trainees;user id=test;password=Test"))
    {
        using (SqlCommand cmd = new SqlCommand("select Dname from DEPTDHII where Dname like '%'+@SearchText+'%'", con))
        {
            con.Open();
            cmd.Parameters.AddWithValue("@SearchText", DName);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                result.Add(dr["DName"].ToString());
            }
            return result;
        }
      }
   }

}

浏览本网站 - &gt; http://www.c-sharpcorner.com/UploadFile/29d7e0/jquery-autocomplete-getting-data-from-database-in-Asp-Net/