简体   繁体   中英

Gettin the value from a selected option in Selectize.js

I am using multiselect in selectize.js, how do i get the vales from the select options: html:

<div class="selectBox1" multiple="multiple">
        <select id="period">
          <option value="day">Day</option>
          <option value="week">Week</option>
          <option value="month">Month</option>
          <option value="year">Year</option>
        </select>

select.js init:

$(document).ready(function($){
  
  $('#period').selectize({
  sortField: "text",
  placeholder: "Select a Period..",
});
});

I tried to get the value by simply using this code:

var values = $('#period').val();
console.log(values)

but it returns an empty array. what can i do?

Just add onChange handler:

$(document).ready(function($){
  
  $('#period').selectize({
  sortField: "text",
  placeholder: "Select a Period..",
  onChange(value) {
    console.log(value);
  }
});
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM