jquery - Autosuggest tag-it jquery - 如何在回发时获取 ID 和标题?

标签 jquery asp.net autosuggest tag-it

我正在使用这个自动建议插件:http://aehlke.github.com/tag-it/

我正在从数据库中获取一个项目数组(现在只是一个普通数组)。该列表包括 ID 和标题。当我提交表单时,我想同时获得 ID 和 Title。现在我只能获得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" />

欢迎任何帮助。谢谢。

最佳答案

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

您需要的第一件事是,在检索时,将 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.
}

关于jquery - Autosuggest tag-it jquery - 如何在回发时获取 ID 和标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12027319/

相关文章:

javascript - 使用 JQuery Mobile 按钮触发 Rest API 调用

javascript - 自动建议两个输入框

c# - 如何将参数从 javascript 传递到通用处理程序?

JavaScript 自动建议更新多个字段

php - mysql - 在逗号分隔的文本字段中查找匹配项

javascript - CSS <div> 换行错误

jquery - Animate.css 网站颜色渐变动画

jquery - d3.js Treemap - 有没有办法指定显示首选项(排序)?

c# - ASP.NET MVC - 将图像上传到 Amazon S3

javascript - 如何编辑和更新pdf文件?