select2取值報錯,Failed to read the 'selectionDirection' property from 'HTMLInputElement': The input element's type ('hidden') does not support selection.


用到了 select2 組件來多選收件人,用搜狗瀏覽器(6.2版高速模式)在執行到如下這句時報錯(Uncaught InvalidStateError: Failed to read the 'selectionDirection' property from 'HTMLInputElement': The input element's type ('hidden') does not support selection.),6.3版高速和兼容模式都沒問題。

var recipients = JSON.stringify($("select#recipient").select2('data'));

咋一看好像是 input[hidden] 不支持 selection,搞不明白,還是先上 stackoverflow,居然有人遇到同樣的問題

http://stackoverflow.com/questions/25487927/javascript-stringify-object

解決辦法:

//var recipients = JSON.stringify($("select#recipient").select2('data')); //搜狗6.2.5.21637高速模式下報錯
var selectedObjects = $("select#recipient").select2('data');
var dataToSave = [];
$.each(selectedObjects, function (index, obj) {
  dataToSave.push({ "id": obj.id, "text": obj.text });
});
var recipients = JSON.stringify(dataToSave);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM