2014-04-30 48 views
0

我初始化與遠程數據(JSON)SELECT2(多個),並且當表單提交SELECT2 IDS提交所選的文本(或多個)的索引,可以將它返回所選擇的文本(在逗號等分離索引)而不是索引?選擇二過帳,而不是文本

<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"}] 

回答

0

我相信你需要的是:

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

這應該給你的JSON的是什麼改變時裏面選擇。

+0

這將返回所選內容的實例的JSON:[{ 「ID」: 「0」, 「文」: 「會計」},{ 「ID」: 「1」, 「文」: 「會計與金融」 }],我只想使用「文本」對象。那麼我怎樣才能得到文字?就像在上述情況下,「會計學,會計與金融」和JSON.parse被返回undefined,因爲重複鍵的可能? –

+0

太糟糕了解析不工作,你可能不得不求助於串手動處理得到的文本。 –

相關問題