首页 > 解决方案 > 使用 select2 时,选择框自动隐藏

问题描述

<div class="col-12 col-sm-12 col-md-5 col-lg-5 col-xl-5" id="old_customer" style="display: block;>
    <label for="customersID" class="control-label">Customer </label>
    <select name="customersID"  id="customersID" onchange="getCustomersDue()"  >
        <option value="">Select Customer </option>
        <?php
        foreach($customers as $p)
        {
            $selected = ($p['customersID'] == $this->input->post('customersID')) ? ' selected="selected"' : "";

            echo '<option value="'.$p['customersID'].'" '.$selected.'>'.$p['name']."-".$p['mobile'].'</option>';
        }
        ?>
    </select>

 $("#customersID").select2();

我正在从数据库中获取所有客户,并使用 select2 搜索并选择项目。但是当我选择某些东西时,整个 div 会隐藏,我无法重新选择任何东西。但该值仍保留在页面中。任何解决方案都会有所帮助。谢谢。

标签: phpjqueryjquery-select2

解决方案


您需要使用下面的代码进行 onchange。删除此代码onchange="getCustomersDue()"

$('#customersID').on('select2:select', function(e) {
    console.log('Selecting: ' , e.params.args.data);
});

读这个

https://select2.org/programmatic-control/events


推荐阅读