gpt4 book ai didi

javascript - 如何获取所选选项的标签

转载 作者:行者123 更新时间:2023-11-30 20:57:51 25 4
gpt4 key购买 nike

我有一个多选下拉菜单,我正在尝试从所选选项的标签中获取完整的文本。

<select role=select multiple= aria-multiselectable=true class=editable inline-edit-cell ui-widget-content ui-corner-all style=display: none;>
<option value=1 label="UNIVERSAL (aa)"></option>
<option value=2 label="UNIVERSAL (bb)"></option>
<option value=3 label="UNIVERSAL (ccc)"></option>
<option value=4 label="UNIVERSAL Without DT"></option>
</select>

{name:"license", id :"qqq", label:"License", width:"400",editrules: true, edittype:"select",Overflow: "visible",
editoptions: {
dataInit: function (elem) {
$(elem).multiselect({
includeSelectAllOption: true,
enableFiltering: true,
maxWidth: 400, //'auto',
maxHeight: 135,
multiselect:true,
selectedList: 3,
noneSelectedText: "Please select",

onChange: function() {
var selected = this.$select.val();
var selectTxt = $('select option:selected').prop('label');
console.log("selectTxt "+selectTxt );

return ;

},
});

$(elem).multiselect('dataprovider',platdata);

},

这里 selectTxt 得到的是 UNIVERSAL,它应该是 UNIVERSAL (aa)

最佳答案

onchange 函数返回属性 value 并使用该值作为索引,我可以访问第 n 个选项并获取 label

$("#select").change(function() {
var index = $(this).val();
for (var i = 0; i < index.length; i++){
var label = $("#select option:nth-child("+index[i]+")").attr("label")
console.log(label);
}

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple=multiple id="select">
<option value=1 label="UNIVERSAL (aa)"></option>
<option value=2 label="UNIVERSAL (bb)"></option>
<option value=3 label="UNIVERSAL (ccc)"></option>
<option value=4 label="UNIVERSAL Without DT"></option>
</select>

关于javascript - 如何获取所选选项的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47467807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com