我正在使用select2控件,loading data通过ajax.这需要使用<input type=hidden..>
标签.
Now, I want to retrieve the selected text. (The value
property in the data-bind
expression sotres the id
only)
我已经try 了$(".select2-chosen").text()
个,但当我在页面上有多个select2控件时,这会中断.
我正在使用select2控件,loading data通过ajax.这需要使用<input type=hidden..>
标签.
Now, I want to retrieve the selected text. (The value
property in the data-bind
expression sotres the id
only)
我已经try 了$(".select2-chosen").text()
个,但当我在页面上有多个select2控件时,这会中断.
从2到4.x、 它总是返回一个数组,即使对于非多选列表也是如此.
var data = $('your-original-element').select2('data')
alert(data[0].text);
alert(data[0].id);
Select 2 3.x及以下
单选:
var data = $('your-original-element').select2('data');
if(data) {
alert(data.text);
}
请注意,如果没有 Select ,变量"data"将为null.
多选:
var data = $('your-original-element').select2('data')
alert(data[0].text);
alert(data[0].id);
alert(data[1].text);
alert(data[1].id);
从3.x docs开始:
data Gets or sets the selection. Analogous to val method, but works with objects instead of ids.
data method invoked on a single-select with an unset value will return null, while a data method invoked on an empty multi-select will return [].