【问题标题】:Select2 Posting Ids instead of textSelect2 发布 ID 而不是文本
【发布时间】:2014-06-17 06:22:30
【问题描述】:

我正在使用远程数据 (json) 初始化 select2(multiple),当提交表单时 select2 提交所选文本的索引,它是否可以返回所选文本(以逗号分隔,如索引)而不是索引?

 <input type="hidden" id="IntendedCourses" name="IntendedCourses"/>

 $.getJSON("GetCourses.aspx", function (course) {               
                $("#IntendedCourses").select2({
                    multiple: true,
                    data: course
                });    
            });

json格式:

[{"id":"0","text":"Accounting"},{"id":"1","text":"Accounting & Finance"},{"id":"2","text":"Aeronautical"},{"id":"3","text":"Aerospace Engineering"}]

【问题讨论】:

    标签: asp.net jquery-select2


    【解决方案1】:

    我相信你需要的是:

    $("#IntendedCourses").change(function() {
        var selection = (JSON.Stringify($("#IntendedCourses").select2('data')));
    });
    

    这应该会在更改时为您提供所选内容的 JSON。

    【讨论】:

    • 这将返回所选内容的 JSON,例如:[{"id":"0","text":"Accounting"},{"id":"1","text": "Accounting & Finance"}] 我只想使用 "text" 对象。那么我如何只获取文本?就像上面的例子一样,“Accounting,Accounting & Finance”和 json.parse 返回未定义,可能是因为重复的键?
    • 太糟糕的解析不起作用,您可能不得不手动求助于字符串处理来获取文本。
    猜你喜欢
    • 1970-01-01
    • 2015-02-03
    • 2022-10-21
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    相关资源
    最近更新 更多