[英]can i pass values to this Jquery var for autocomplete() from DB.?
$(function () {
var availableTags = [
"ActionScript", "AppleScript","Asp","BASIC",
"C","C++","Clojure","COBOL","ColdFusion","Erlang",
"Fortran","Groovy","Haskell","Java","JavaScript","Lisp",
"Perl","PHP","Python","Ruby","Scala","Scheme"];
$("#mdatepicker").autocomplete({
source: availableTags
});
});
上面給出的摘錄來自jquery.orq
我可以制作一個數組(應從ASP.NET中的DB-MS SQL Server檢索到的可用產品)嗎?
您必須編寫一個返回JSON的方法,然后在您的jquery腳本上調用此方法,並使用結果構建js數組。
有很多方法可以做到這一點,我認為最簡單的方法是添加一個u字段,該字段由u創建服務器端,其中將包含從數據庫獲取的值。
//connect to your database and retreive your data and insert it into hf.value.
hF.Value = "the values retreived must be separated by a ',' "
hF.ID = "hF";
// add your control to the webpage.
和jQuery將是
$(function () {
var availableTags = $("#hF").val().split(',');
$("#mdatepicker").autocomplete({
source: availableTags
});
});
希望這可以幫助。
List<string> myAutoList = new List<string>() ;
myAutoList.Add("Grand Trust");
myAutoList.Add("iSmart");
myAutoList.Add("F5 Tech");
StringBuilder script = new StringBuilder();
script.Append("var availableTags = [ ");
foreach (string str in myAutoList )
{
script.Append("'");
script.Append(str.ToString());
script.Append("', ");
}
script.Remove(script.Length - 2, 2);
script.Append(" ];");
ClientScript.RegisterClientScriptBlock(GetType(), "MyScript", script.ToString(), true);
並且在Javascript中需要相同:
$(function () {
// if (availableTags == null)
// {
// var availableTags = [
// 'ActionScript', "AppleScript", "Asp", "BASIC","C","C++","Clojure","COBOL","ColdFusion","Erlang","Fortran","Groovy","Haskell","Java",
// "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme"];
// }
$("#mdatepicker").autocomplete({
source: availableTags
});
});
最好的方法是使用AJAX調用。
在ASP.NET應用程序中,您應該具有WCF Web服務的.ASMX,它可以返回實體數組以進行自動完成。
只是一個示例代碼,可能會指導您。
服務器端(WCF):
public IList<Product> GetProductsStartWith(string productName) {
// ask db here and return results
return productList;
}
在客戶端,您必須查詢Web服務以獲取數據,
var startWith = $('#input').val();
$.getJson('/WebService/GetProductsStartWith', startWith, function(response) {
$("#mdatepicker").autocomplete({
source: response.d
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.