实现多个 Select

Materialize Multiple Select

我有一个 Materialise Multiple Select 元素。但我似乎无法找到检索 selected 值的方法。有人知道如何检索 selected 值吗?

这是 select 时的样子:

这是关闭它时的样子:

但问题是如何检索 selector 关闭时的值。

编辑:我正在使用 Materialise CSS (http://materializecss.com)

基本就可以得到jQuery; Material 标记没有什么特别之处。

对于这样的select:

<select id="mySelect" multiple>
    <option value="1" selected>Option 1</option>
    <option value="2" selected>Option 2</option>
    <option value="2">Option 2</option>
</select>

$('#mySelect').val() 将 return 一个 select 值数组:["1", "2"]

试试看(获得更多 select 标签)

var datas = {};// object name=> value
$("form").find("select.initialized").each(function() {
    var value_input_select = $(this).parents(".select-wrapper").find("input.select-dropdown").val()
    value_input_select = value_input_select.replace(/&nbsp;/gi,"").replace(/ /gi,"")
    var value_input_select_array = value_input_select.split(",")
    var value_string = ""
    $(this).find("option").each(function(){
      if(value_input_select_array.indexOf($(this).text().replace(/&nbsp;/gi,"").replace(/ /gi,"")) >= 0){
        value_string += $(this).attr("value")+","
      }
    })
    if(value_string != ""){
      value_string = value_string.substring(0, value_string.length-1)
    }
    datas[this.name] = value_string;
  });

请尝试一下:在这里您将找到一种从多个 select 中检索和删除值的适当方法。实际上materialize是用UL和LI来显示一个select。这就是它产生错误的原因。

$(document).ready(function () {
    $('select').material_select();
    $('select').change(function(){
        var newValuesArr = [],
        select = $(this),
        ul = select.prev();
        ul.children('li').toArray().forEach(function (li, i) {
            if ($(li).hasClass('active')) {
                newValuesArr.push(select.children('option').toArray()[i].value);
            }
        });
        select.val(newValuesArr);

        console.log($(this).val());
    });
});

http://jsfiddle.net/9bL25jw9/59/