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);
原文地址:https://www.cnblogs.com/felixnet/p/5833531.html