javascript - unitips - Obtenga palabras de la base de datos y páselos a un archivo de script que se ejecuta en el navegador
unitips log in (1)
<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>
Intenté esto pero no obtener resultado es algo malo aquí soy nuevo en JavaScript voy a autocomplatar mi área de texto funciona en palabras: [''apple'', ''google'', ''facebook'', ''github'']
, pero cuando recupero desde db no obteniendo resultado
Mira esto, puede ayudarte
<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;
}
}
}
}
eche un vistazo a este sitio -> http://www.c-sharpcorner.com/UploadFile/29d7e0/jquery-autocomplete-getting-data-from-database-in-Asp-Net/