gpt4 book ai didi

javascript - select2 获取触发选择/取消选择的选项值

转载 作者:太空宇宙 更新时间:2023-11-04 15:30:40 25 4
gpt4 key购买 nike

https://select2.github.io/options.html#what-events-will-select2-trigger

文档说可以防止 select2:selectingselect2:unselecting 。我想根据选择/取消选择的值有条件地阻止该事件。

$('select').on('select2:selecting', function(e) {
// what value is currently being selected?
// $(this).val() does not yet have it and I should set it conditionally
})

最佳答案

有关所选选项的所有信息都包含函数的 e 变量,因此您可以像这样获取它:

$('select').select2().on('select2:selecting', function(e) {
console.log(e.params.args.data.id); //value of selected option
console.log(e.params.args.data.text); // text of selected option
});

Check this jsFiddle .

关于javascript - select2 获取触发选择/取消选择的选项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44819709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com