use*_*474 5 javascript c# asp.net jquery jquery-ui
我有一个js代码,其中一个数组运行良好
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C"
];
Run Code Online (Sandbox Code Playgroud)
然后我在我的代码后面创建了一个数组字符串,或者在类级别上创建.cs
public static string[] test={"animal","lovely"};
Run Code Online (Sandbox Code Playgroud)
然后我将js数组更改为此
var availableTags = "<%=test%>"; // also tried without quotes
Run Code Online (Sandbox Code Playgroud)
现在我没有像以前的js数组一样的结果
使用完整的代码进行编辑,我从http://jqueryui.com/demos/autocomplete/#multiple中获取了jquery
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Script.Serialization;
public partial class onecol : System.Web.UI.Page
{
JavaScriptSerializer serializer;
public static string test = "['animal','lovely']";
public static string check;
protected void Page_Load(object sender, EventArgs e)
{
serializer = new JavaScriptSerializer();
//serializer
this.detail.ToolsFile = "BasicTools.xml";
test = returnTitle();
}
}
Run Code Online (Sandbox Code Playgroud)
并且带有html的脚本是
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript" src="Jscript.js"></script>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.17.custom.css"></script>
<link href="~/jquery-ui-1.8.17.custom.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
$(function () {
var availableTags = <%=test%>;
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
$("#tags")
// don't navigate away from the field on tab when selecting an item
.bind("keydown", function (event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this).data("autocomplete").menu.active) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function (request, response) {
// delegate back to autocomplete, but extract the last term
response($.ui.autocomplete.filter(
availableTags, extractLast(request.term)));
},
focus: function () {
// prevent value inserted on focus
return false;
},
select: function (event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="demo" >
<div class="ui-widget">
<label for="tags">Tag programming languages: </label>
<input id="Text1" class="#tags" size="50" />
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
实际上它是一个自动完成功能来提供标签,我希望从C#代码获取标签的自动完整建议,我从jqueryui.com/demos/autocomplete/#multiple获取Jquery源,然后我尝试从它给它C#字符串. cs文件,我用编辑版本的代码解释它,后面的C#代码与它在链接中的工作方式完全相同
Bri*_*ian 23
您需要将C#字符串数组序列化为javascript数组.
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
通常我会创建一个简单的静态类作为包装器.
public static class JavaScript
{
public static string Serialize(object o)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(o);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用该类序列化您需要的项目
//C# Array(Member of asp page)
protected string[] Values = { "Sweet", "Awesome", "Cool" };
<script type="text/javascript">
var testArray = <%=JavaScript.Serialize(this.Values) %>
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31688 次 |
| 最近记录: |