[英]How do I get the custom attribute of a multiple select
Does anyone know how to get the value of a custom attribute "custom" from a select multiple? 有谁知道如何从多个选择中获取自定义属性“ custom”的值? I think you would get an array by selecting several elements 我认为您可以通过选择几个元素来获得一个数组
<select class="selectpicker" multiple>
<option custom="Mustolest">Mustard</option>
<option custom="Kellared">Ketchup</option>
<option custom="Reloaded">Relish</option>
</select>
I'm using bootstrap select. 我正在使用引导选择。 What I have tried is the following: 我尝试了以下内容:
$(function() {
$('.selectpicker').selectpicker();
$('.selectpicker').on("change",function() {
console.log($('.selectpicker').selectpicker("val"));
});
})
With this I get the values in the form of an array, correct. 这样我就可以得到数组形式的值,正确的。 But I need to get over a custom attribute. 但是我需要克服一个自定义属性。 The options that you have given me would not be working for me. 您给我的选择将对我不起作用。
Exactly: Get the array of the selected elements and get their respective custom attributes, like this: 确切地说:获取所选元素的数组并获取它们各自的自定义属性,如下所示:
$("select :selected").map((i, el) => $(el).attr("custom")).toArray()
This will return something like this: 这将返回如下内容:
Array [ "Mustolest", "Kellared" ]
Implemet below script, using below script display selected custom options values. Implemet下面的脚本,使用下面的脚本显示选定的自定义选项值。
script code: 脚本代码:
$(".selectpicker").on("change", function(){ var optionValues= []; $.each($(".selectpicker option:selected"), function(){ optionValues.push($(this).val()); }); $("#DisplaySeletedOptions").html(optionValues+','); });
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="selectpicker" multiple> <option custom="Mustolest">Mustard</option> <option custom="Kellared">Ketchup</option> <option custom="Reloaded">Relish</option> </select> <div id="DisplaySeletedOptions"></div>
I hope using this script resolved your problem. 我希望使用此脚本可以解决您的问题。
You have to use data-custom
instead of custom
attribute , see doc , If you're using bootstrap select so , to get values just use $('.selectpicker').selectpicker("val")
to get values , in order to get custtuom attrib use 您必须使用data-custom
而不是custom
属性,请参阅doc ,如果您使用的是bootstrap select so, $('.selectpicker').selectpicker("val")
获取值,只需使用$('.selectpicker').selectpicker("val")
来获取值,即可取得使用权
let selected = $('.selectpicker').find('option:selected')
.map(function(index,element){
return $(element).attr("data-custom");
//or return $(element).data("custom"))
}).toArray();
see below working snippet 参见下面的工作片段
$(function() { $('.selectpicker').selectpicker(); $('.selectpicker').on("change",function() { //console.log($('.selectpicker').selectpicker("val")); let selected = $('.selectpicker').find('option:selected') .map(function(index,element){ return $(element).attr("data-custom"); //or return $(element).data("custom")) }).toArray(); console.clear(); console.log(selected); }); })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.9/dist/css/bootstrap-select.min.css"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <!-- Latest compiled and minified JavaScript --> <script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.9/dist/js/bootstrap-select.min.js"></script> <select class="selectpicker" multiple> <option data-custom="Mustolest">Mustard</option> <option data-custom="Kellared">Ketchup</option> <option data-custom="Reloaded">Relish</option> </select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.