首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何获取jquery中select2元素的最后一个被选中或未被选中的元素?

如何获取jquery中select2元素的最后一个被选中或未被选中的元素?
EN

Stack Overflow用户
提问于 2019-06-12 22:08:03
回答 1查看 125关注 0票数 1

我想让我的select2 .I的最后一个选中或未选中的元素遵循官方文档,但happens.This不是我的代码:

代码语言:javascript
运行
复制
jQuery(document).ready(function() {
        Main.init();
        var $eventSelect = $('.search-select'); //select your select2 input
        $(".search-select").select2({
            allowClear: true
        });
        $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
        })
        $(".search-select").on('select2:select', function(e) {
            console.log(e.params.data.id);
        });
    });


 <select id="bird_id_1" name="bird_id_1[]" class="form-control search-select selects" multiple required>

 @foreach($birds as $bird)                                                   
     <option value="{{ $bird->id }}" {{ in_array($bird->id, App\Http\Controllers\ClaimController::getbirdsids($claim->id)) ? 'selected' : '' }}>{{ $bird->title_fr }}</option>                                           
 @endforeach

 </select>
EN

回答 1

Stack Overflow用户

发布于 2019-06-13 00:08:52

这是让一切正常工作的最终代码。

代码语言:javascript
运行
复制
$("select.selects").on("select2-selecting", function (e){
        console.log(e.object.text);
        $(".input_nb:last").after("<label class='"+e.val+" input_nb'>Le nombre d’oiseau pour:"+e.object.text+"</label><input type='text' class='form-control input_nb "+e.val+"' name='num_espece[]'   placeholder=''>");   
    });
    $("select.selects").on("change", function (e){
        console.log(e.removed.id)
        if(e.removed)
        {
            $("."+e.removed.id).remove();
        }

    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56564171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档