我有一个js代码,其数组在
之类的情况下效果很好var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C"
];
然后我在我的代码后面创建了一个数组字符串,或者在类级别<。p>上创建.cs
public static string[] test={"animal","lovely"};
然后我将js数组更改为此
var availableTags = "<%=test%>"; // also tried without quotes
现在我没有像以前的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();
}
}
并且带有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>
实际上它是一个自动完成功能来提供标签,我希望从C#代码获取标记的自动完整建议,我从jqueryui.com/demos/autocomplete/#multiple中获取Jquery源代码然后我尝试给它C#来自.cs文件的字符串,我用编辑版本的代码解释它,后面的C#代码与链接中的完全相同
答案 0 :(得分: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);
}
}
然后您可以使用该类序列化您需要的项目
//C# Array(Member of asp page)
protected string[] Values = { "Sweet", "Awesome", "Cool" };
<script type="text/javascript">
var testArray = <%=JavaScript.Serialize(this.Values) %>
</script>
答案 1 :(得分:6)
var availableTags = ['<%=String.join("','",test)%>'];
答案 2 :(得分:3)
会是这样的......
var availableTags = ["<%= string.Join("\", \"", test) %>"];
或
var availableTags = ['<%= string.Join("', '", test) %>'];
第一个将呈现为
var availableTags = ["Sweet", "Awesome", "Cool"];
,第二个将呈现为
var availableTags = ['Sweet', 'Awesome', 'Cool'];
两者都可以自动完成。
答案 3 :(得分:1)
test
属性应该是一个字符串属性,它应该呈现将由Js引擎作为数组解析的字符串。试试这个。
财产后面的代码
public static string test= "['animal','usman lovely']";
JS
var availableTags = <%=test%>;//No quotes
答案 4 :(得分:0)
仅使用索引来获取测试字符串数组 我试过给像
这样的索引var availableTags = "<%=test[0]%>"; // animal
var availableTags = "<%=test[1]%>"; // lovely