selectize.js : to clear selected value in onChange event

血红的双手。 提交于 2019-12-10 17:40:29

问题


I am trying to clear out the value selected in the onChange event depending on a condition. Whether or not its already present in a selected list. However i am not able to clear of the value using
$select[name][0].selectize.clear(); from inside the onChange object. I tried various versions of it , like using the elements id. Please help me with this.

$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
     }
 }
});

回答1:


This is the solution I found.

     /* 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 {
}
},


来源:https://stackoverflow.com/questions/27954990/selectize-js-to-clear-selected-value-in-onchange-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!