select2默认的data 是这样的格式
var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
但是我还有一个typeId 想要加在上面,select2有没有什么办法达到这个效果?
就是下面这种效果
<option value='id' type='typeId'>text</option>
data.push()
看看有没有帮助
| 清空下拉框列表值
$("#select2-id").empty();
| 设置下拉列表
复制代码
// 单选 - 必须有一项为空值,否则默认选择第一项(如果必须选择一项可以不设置空值)
$("#select2-id").append($("<option>", {value: '', text: '全部'}));
$("#select2-id").append($("<option>", {value: 'value1', text: 'text1'}));
$("#select2-id").append($("<option>", {value: 'value2', text: 'text2'}));
// 多选 - 不能有一项为空值,否则再清空时会出BUG
$("#select2-id").append($("<option>", {value: 'value1', text: 'text1'}));
$("#select2-id").append($("<option>", {value: 'value2', text: 'text2'}));
复制代码
| 赋值说明:赋值会触发change事件
// 赋值 - 单选
$("#select2-id").val('value').trigger("change");
// 赋值 - 多选
$("#select2-id").val(['value1','value2']).trigger("change");
| 获取选中值
// 多选返回数组,单选返回字符串
$("#select2-id").val();