如何获取所选选项的标签

时间:2017-11-24 06:55:12

标签: javascript jquery

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

keyup

此处selectTxt的格式为<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); }, ,应为UNIVERSAL

1 个答案:

答案 0 :(得分:1)

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>