我有以下selectize.js

  $("#abc").selectize({
      valueField: 'name',
      labelField: 'name',
      searchField: ['name'],
      plugins: ['remove_button'],
      createOnBlur: true,
      maxItems: 10,
      preload: true,
      create:function (input){
        callback({ name:'Cricket', name:'Football', name : 'Tennis'})
      },
  });


html代码是:

<select id="abc" name="sport[]"></select>

我希望CricketFootballTennis预先加载,这意味着应该在页面加载时自动加载。但是选项未加载,我缺少什么?

最佳答案

似乎您可能只是在寻找options属性来设置选择选项的初始列表,而寻找items属性来选择要默认的选项。如果是这样,则不需要create功能(可以使用该功能自定义对用户输入的选项的处理)。见下文。



$('#abc').selectize({
  options: [
    {name: 'Cricket'},
    {name: 'Football'},
    {name: 'Tennis'}
  ],
  items: [
    'Cricket',
    'Football',
    'Tennis'
  ],
  valueField: 'name',
  labelField: 'name',
  searchField: ['name'],
  plugins: ['remove_button'],
  createOnBlur: true,
  maxItems: 10,
  preload: true
});

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Selectize</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.default.min.css">
  </head>

  <body>

    <form action="" method="POST">
      <input id="abc" name="abc" type="text" />
      <button type="submit">Submit</button>
    </form>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.js"></script>

  </body>

</html>

09-17 19:00