Different display value for selecte text using select2.js

后端 未结 5 2135
闹比i
闹比i 2021-01-18 15:24

Trying to implement a custom select dropdown using select2 plugin Is it possible to have the selected value to display only the actual option \'value\' instead of the text,

5条回答
  •  星月不相逢
    2021-01-18 15:56

    Simply use the formatSelection option. It provides the text and value of the selected option and sets the selection's text.

    function formatSelection(val) {
      return val.id;
    }
    
    $('select').select2({
      formatSelection: formatSelection,
      width: 300
    });
    

    Fiddle

提交回复
热议问题