Select2 jquery - 如何在selectbox中获取文本 [英] Select2 jquery - How to get text in selectbox
问题描述
我有输入文字,如
<input type="hidden" name="xa" id="xa" data-placeholder=".." style ="width: 360px;"/>
我使用select2
and i using select2
$("#xa").select2({...});
现在,我希望在按获取文字时获取文字
now, i want to get text when i press 'get text'
$("#gettext").click(function () {
alert($('#xa').val()); // or $('#xa').select2("val");
});
但是它得到了文本的id。这不是文字。我该怎么做。谢谢
But it get the id of text. It's not text. How can i do it. thanks
推荐答案
Select2
创建数据 -
属于内部用途,如果它存储了一些必要的信息,如 id
,选择值
select2
实例。这可用于提取选定值
。
Select2
creates a data-
attribute for internal purpose, were it stores some necessary information like id
,selected value
of select2
instance. this can used to extract selected value
.
Select2社区:实例化为
select2
组件的每个元素必须包含id和text密钥。
Select2 Community : Each element instantiated as a
select2
component must contain id and text keys.
var id = $(test).select2('data')。id ;
var seletedVal = $(test).select2('data')。text ;
更新:使用最新版本的Select2,对象存储在一个数组中,因此必须按如下方式访问文本( jsfiddle链接也已更新
)。
Update : With Latest version of Select2 , the object is stored in a array,so the text has to be accessed as below (jsfiddle link updated as well
).
$(test).select2('data ')[0] .text
//而不是$(test).select2('data')。text
$(test).select2('data')[0].text
//instead of $(test).select2('data').text
感谢更新@ DiMono
thanks for update @DiMono
快乐编码:)
这篇关于Select2 jquery - 如何在selectbox中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!