jQuery select2 control - retrieve last selected element

一个人想着一个人 提交于 2019-12-01 09:09:38

for the second part this may help you:

$(this).val(); // references the select

you can filter it to get all needed values.

A Example is in the fiddle: http://jsfiddle.net/jEADR/1588/

Oscar Vazquez

Hey I might be a little late answering this but I found a pretty easy solution to this. You we're right by looking through the event for the last selected item. This worked for me.

var $eventSelect = $('.select_field'); //select your select2 input
$eventSelect.on('select2:unselect', function(e) {
  console.log('unselect');
  console.log(e.params.data.id); //This will give you the id of the unselected attribute
  console.log(e.params.data.text); //This will give you the text of the unselected text
})
$eventSelect.on('select2:select', function(e) {
  console.log('select');
  console.log(e.params.data.id); //This will give you the id of the selected attribute
  console.log(e.params.data.text); //This will give you the text of the selected
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!