我正在使用这个自动建议插件:http ://aehlke.github.com/tag-it/
我从数据库中获取一组项目(现在只是一个普通数组)。该列表包括 ID 和标题。当我提交表单时,我想同时获得 ID 和 Title。现在我只能获得标题。我想获取这两个值,以便可以创建新的引用(ID = 0),并且可以插入现有的引用而无需任何数据库查找。
这是我的代码。
book.aspx - book.aspx.cs 的代码隐藏:
...
protected void btnSave_Click(object sender, EventArgs e)
{
Response.Write(txtReferences.Text); // this contains Titles, but I would like both values.
}
public class Reference
{
public string Title;
public int ID;
}
[WebMethod]
public static Array GetReferences(string title)
{
// this will be replaced by lookup in database.
List<Reference> References = new List<Reference>{
new Reference{ID=1, Title="My ref 1"},
new Reference{ID=2, Title="Ref ref 2"},
new Reference{ID=3, Title="Blah ref 3"},
new Reference{ID=0, Title=title} // for new tags set ID 0 to indicate that this should be created in db
};
return References.ToArray();
}
....
这是我当前的脚本:
<script type="text/javascript">
$(document).ready(function () {
var failure = function (result) {
alert(result.status + " " + result.statusText);
}
var ref = $("#<%=txtReferences.ClientID %>");
ref.tagit({
allowSpaces: true,
removeConfirmation: true,
tagSource: function (title, showChoices) {
var params = '{"title":"' + title.term + '"}';
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: failure,
url: "book.aspx/GetReferences",
data: params,
success: function (data) {
var assigned = ref.tagit("assignedTags");
var filtered = [];
for (var i = 0; i < data.d.length; i++) {
if ($.inArray(data.d[i].Title, assigned) == -1) { filtered.push(data.d[i].Title); }
}
showChoices(filtered);
}
});
}
});
});
</script>
当然,我在 book.aspx 页面上有一个文本框:
<asp:TextBox ID="txtReferences" runat="server" />
欢迎任何帮助。谢谢。