0

我使用Select2 jquery 插件为我的 ASP.NET MVC 网站上的帖子添加/创建标签。我使用的功能是“加载远程数据”和“标记支持”,如下所示:

        <script type="text/javascript">

        function tagResultList(tag) {
            var counter = 'ny tagg';

            var markup = "<table class='movie-result'><tr>";
            markup += "<td class='movie-info'><div class='movie-title'>" + tag.text

            if (typeof tag.count != 'undefined') {
                counter = tag.count;
            }

            markup += "(" + counter + ")</div>";
            markup += "</td></tr></table>"
            return markup;
        }

        function tagResultSelectionName(tag) {
            return tag.text;
        }


        $("#txtTagBox").select2({
            multiple: true,
            createSearchChoice:
                function (term, data) {
                    if ($(data).filter(function () { return this.text.localeCompare(term) === 0; }).length === 0) {
                        return { id: 0, text: term };
                    }
                },
            placeholder: "Sök efter en tagg",
            minimumInputLength: 3,
            maximumInputLength: 30,
            maximumSelectionSize: 5,
            ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
                url: _tagsUrl,
                dataType: 'json',
                quietMillis: 100,
                data: function (term, page) {
                    return {
                        q: term, // search term
                        page: page
                    };
                },
                results: function (data, page) { // parse the results into the format expected by Select2.
                    // since we are using custom formatting functions we do not need to alter remote JSON data
                    return { results: data.Tags, more: data.MorePages };
                }
            },
            formatResult: tagResultList, // omitted for brevity, see the source of this page
            formatSelection: tagResultSelectionName,  // omitted for brevity, see the source of this page
            dropdownCssClass: "bigdrop" // apply css that makes the dropdown taller
        });
    </script>

我可以键入并选择一个新标签(创建新标签),但问题是提交文本框时会将 Id 0 转发到服务器,并且无法知道创建的标签的名称。

那么如何让它将字符串而不是 id 发送到服务呢?或者是否存在混合模式,例如现有的 id 和创建的字符串?

此致

4

1 回答 1

0

尝试

 function (term, page) {
                return {
                    q: term, // search term
                    page: page
                    ,newtag : 'created tag' //replace by var or jquery chain that'll return the string 
                }

它将在 get 数组中发送到带有索引 newtag 的 _tagsurl

于 2013-02-09T10:21:48.810 回答