jquery 如何获取所选选项的标签

ru9i0ody  于 2023-10-17  发布在  jQuery
关注(0)|答案(2)|浏览(115)

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

<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)

rbl8hiat

rbl8hiat1#

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>
dba5bblo

dba5bblo2#

mySelectElement.querySelector(`option[value='${mySelectElement.value}']`).getAttribute("label")

相关问题