如何从多个选择的选项中获取数据属性?

时间:2019-04-27 14:03:03

标签: javascript jquery

如何从多个选择中获取选择值的列表? 我只能得到value( 我需要获取attr('data-name')

https://playcode.io/301780?tabs=console&index.html&output

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="payment" style="width: 246px;" required multiple="multiple" class="select2" id="id_payment">
  <option value="3" data-name="blago">blago</option>
  <option value="4" data-name="usd" selected>usd</option>
  <option value="5" data-name="uah" selected>uah</option>
  <option value="2" data-name="btc">btc</option>
</select>

<script>
  $(document).ready(function() {
    var vals = $('#id_payment').val()
    var paymentList = []

    if (vals) {
      for (var i = 0; i < vals.length; i++) {
        console.log(vals[i])
        paymentList.push($(vals[i]).selector)
      }
    }
  })
</script>

4 个答案:

答案 0 :(得分:2)

您可以使用map遍历选定的选项,并使用data获得data-name的值。

var result = $("#id_payment option:selected").map(function() {
  return $(this).data("name");
}).get();

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="payment" style="width: 246px;" required multiple="multiple" class="select2" id="id_payment">
  <option value="3" data-name="blago">blago</option>
  <option value="4" data-name="usd" selected>usd</option>
  <option value="5" data-name="uah" selected>uah</option>
  <option value="2" data-name="btc">btc</option>
</select>

答案 1 :(得分:1)

使用each遍历项目并将它们收集到一个数组中,例如:

var dataNames = [];
$("#id_payment > option[selected]").each(function() {
    dataNames.push($(this).data("name"));
});

答案 2 :(得分:1)

您可以使用此代码

$("#id_payment option:selected").each(function(index, item){

         let name = $(item).data('name');
         //alert(name)
         nameselected.push(name);
         console.log(name);
   })

$(document).ready(function () {
var vals = $('#id_payment').val()
var paymentList = []
var nameselected = []

if (vals) {
    for (var i = 0; i < vals.length; i++) {
        console.log(vals[i])
        paymentList.push($(vals[i]).selector)
    }
  }
  
   $("#id_payment option:selected").each(function(index, item){
         
         let name = $(item).data('name');
         //alert(name)
         nameselected.push(name);
         console.log(name);
   })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="payment" style="width: 246px;" required multiple="multiple" class="select2" id="id_payment">
        <option value="3" data-name="blago">blago</option>
        <option value="4" data-name="usd" selected>usd</option>
        <option value="5" data-name="uah" selected>uah</option>
        <option value="2" data-name="btc">btc</option>
</select>

答案 3 :(得分:1)

您需要使用jquery .each()函数。

示例:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="payment" style="width: 246px;" required multiple="multiple" class="select2" id="id_payment">
  <option value="3" data-name="blago">blago</option>
  <option value="4" data-name="usd" selected>usd</option>
  <option value="5" data-name="uah" selected>uah</option>
  <option value="2" data-name="btc">btc</option>
</select>

<script>
  $(document).ready(function() {
    $('#id_payment :selected').each(function() {
      console.log($(this).data("name"))
    })
    $("#id_payment").click(function(){
      $('#id_payment :selected').each(function() {
        console.log($(this).data("name"))
      })
    })
  })
</script>

它已经设置好,所以当您进行更多选择时,它仍然会记录所有选中的项目