如何初始化ajax上的选择?

时间:2016-08-09 06:29:00

标签: javascript jquery ajax selectize.js

从ajax获取数据后,我必须填充selectize。但它在Post.create({ created_by: ""+Math.random() }) .then(function (result) { console.log('Saved' result) }) .catch(function (err) { console.error('Oh No', err) }) 内部无效。

HTML

ajax success

JS

<select name="hospital" id="store_hospital">
   <option value="" disabled selected>Choose Hospital</option>
   <?php
     foreach ($hospitals as $hospital) {
   ?>
   <option value="<?php echo $hospital->uid ?>"><?php echo $hospital->name ?></option>
   <?php
   }
   ?>
</select>
<select id="select-to" class="contacts" placeholder="Pick some people..."></select>

如何在选择中显示我从$('#store_hospital').on('change', function () { var value = $('#store_hospital').val(); $.ajax({ type: 'GET', url: "/get-store-clients/" + value, success: function (data) { console.info(data); }, error: function (jqXHR, textStatus, errorThrown) { } }); }); $('#select-to').selectize({ persist: false, maxItems: null, valueField: 'email', labelField: 'name', searchField: ['first_name', 'last_name', 'email'], sortField: [ {field: 'first_name', direction: 'asc'}, {field: 'last_name', direction: 'asc'} ], options: [ {email: 'nikola@tesla.com', first_name: 'Nikola', last_name: 'Tesla'}, {email: 'brian@thirdroute.com', first_name: 'Brian', last_name: 'Reavis'}, {email: 'someone@gmail.com'} ], render: { item: function (item, escape) { var name = formatName(item); return '<div>' + (name ? '<span class="name">' + escape(name) + '</span>' : '') + (item.email ? '<span class="email">' + escape(item.email) + '</span>' : '') + '</div>'; }, option: function (item, escape) { var name = formatName(item); var label = name || item.email; var caption = name ? item.email : null; return '<div>' + '<span class="label">' + escape(label) + '</span>' + (caption ? '<span class="caption">' + escape(caption) + '</span>' : '') + '</div>'; } }, createFilter: function (input) { var regexpA = new RegExp('^' + REGEX_EMAIL + '$', 'i'); var regexpB = new RegExp('^([^<]*)\<' + REGEX_EMAIL + '\>$', 'i'); return regexpA.test(input) || regexpB.test(input); }, create: function (input) { if ((new RegExp('^' + REGEX_EMAIL + '$', 'i')).test(input)) { return {email: input}; } var match = input.match(new RegExp('^([^<]*)\<' + REGEX_EMAIL + '\>$', 'i')); if (match) { var name = $.trim(match[1]); var pos_space = name.indexOf(' '); var first_name = name.substring(0, pos_space); var last_name = name.substring(pos_space + 1); return { email: match[2], first_name: first_name, last_name: last_name }; } alert('Invalid email address.'); return false; } }); 获得的值?

1 个答案:

答案 0 :(得分:3)

有添加/更新/删除/清除api https://github.com/selectize/selectize.js/blob/master/docs/api.md 如果你不想重新初始化它

// initialize the Selectize control
var $select = $('select').selectize(options);

// fetch the instance
var selectize = $select[0].selectize;

$ajax.success = function(data) {
  selectize.clearOptions();
  for (var i in data) {
    selectize.addOption(i, data[i]);
  }
  selectize.refreshOptions()
}