【问题标题】:Autosuggest tag-it jquery - how to get the ID AND Title on postback?Autosuggest tag-it jquery - 如何在回发时获取 ID 和标题?
【发布时间】:2012-08-19 14:34:14
【问题描述】:

我正在使用这个自动建议插件: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" />

欢迎任何帮助。谢谢。

【问题讨论】:

  • 如果你在数据库中有标题,我想你可以得到相应的ID!

标签: jquery asp.net autosuggest tag-it


【解决方案1】:

看了 tag-it docssources 之后,恐怕这个插件不支持除了简单的字符串作为标签以外的任何东西,所以如果你想来回发送更多信息,你会必须自己做。假设Title 属性是唯一的(即没有两个标签具有相同的Title 但不同的IDs),但这应该不难......

您需要做的第一件事是,在检索时,将 Title 映射到某处的 ID

var ids = {}
...
                for (var i = 0; i < data.d.length; i++) {
                    ids[data.d[i].Title] = data.d[i].ID;
                    if ($.inArray(data.d[i].Title, assigned) == -1) { ... }
                }

现在您有很多选择。我的建议是使用隐藏字段,每次添加或删除标签时都会更新:

function updateHidden() {
    var chosenTags = $.map(ref.tagit("assignedTags"), function(tag) {
        return { Title:tag, ID:(ids[tag] || 0) }; // If the tag is new, use 0 as the ID
    });
    $("#<%=yourHiddenField.ClientID %>").val(JSON.stringify(chosenTags));
}
...
ref.tagit({
   ...
   onTagAdded:updateHidden,
   onTagRemoved:updateHidden
}

(注意:我建议使用 JSON,但您可以使用任何格式更方便您的服务器处理)

protected void btnSave_Click(object sender, EventArgs e)
{
    Response.Write(yourHiddenField.Text); // this contains both values, encoded in JSON format.
}

【讨论】:

  • @JustinHomes 你需要另一种方法来消除标签的歧义,即当用户在输入中键入“foo”时,有两个标签带有Title“foo”,哪个应该系统选择?
猜你喜欢
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-27
  • 1970-01-01
相关资源
最近更新 更多