selectize.js:清除onChange事件中的选定值

ash*_*h17 4 onchange selectize.js

我试图根据条件清除在onChange事件中选择的值。它是否已经存在于所选列表中。但是我无法
$select[name][0].selectize.clear();从onChange对象内部使用清除值。我尝试了各种版本,例如使用元素id。请帮我解决一下这个。

$select[name].selectize({
valueField: 'lot_name',
labelField: 'lot_name',
create: true,
options: res,
render: {
    option: function (item, escape) {
         return '<div>' +
             '<span><strong>' + item.lot_name + '</strong></span>' +
             '</div>';
     },
     option_create: function (data, escape) {
      return '<div class="create">Map to <strong>' + escape(data.input) + '</strong>&hellip;</div>';
     }
 },
onChange: function (value) {
     if($.inArray(value, selectedList) > -1){
         alert("Lot " + value + " has already been selected. Please recheck your selection and  try again");
    $select[name][0].selectize.clear();
     } else {
          // else logic
     }
 }
});
Run Code Online (Sandbox Code Playgroud)

ash*_*h17 5

这是我找到的解决方案。

     /* Using the Below inside onChange event handler
      var obj = $(this);
      obj[0].setValue("");
     */


onChange: function (value) {
 var obj = $(this);
 if (($.inArray(value, selectedList) > -1)) {
   alert("Lot " + value + " has already been selected. Please recheck your selection and try again");
   obj[0].setValue("");
 } else {
}
},
Run Code Online (Sandbox Code Playgroud)