Select2:禁用的选定选项值不起作用

时间:2019-05-17 00:10:32

标签: php jquery laravel select jquery-select2

我有一个模块,您可以将数量从“仓库从”转移到“仓库到”(“仓库从&仓库到我的下拉菜单”)

我想验证一下您不能再选择第一个来自“仓库来自”的仓库

查看

<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 input-padding-left">
    <div class="form-group">  
        <label>Document Reference</label>
        <input type="text" name="document_reference" class="form-control">
    </div>
    <div class="form-group">
        <label>From:</label>
        <select class="form-control warehouse" name="warehouse_from" required="required" id="warehouse_from" style="width: 100%;">
            <option></option>
            <option value="0">Unassigned Warehouse</option>
            @if($warehouses)
                @foreach($warehouses as $v => $warehouse)
                    <option value="{{ $warehouse->id }}">{{ $warehouse->name }}</option>
                @endforeach
            @endif
        </select>
    </div>
</div>
<hr>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 input-padding-right">
    <div class="form-group">
        <label>Posting Date</label>
        <input type="text" name="posting_date" class="form-control dates">
    </div>
    <div class="form-group">
        <label>To:</label>
        <select class="form-control warehouse" name="warehouse_to" id="warehouse_to" required="required" style="width: 100%;">
            <option></option>
            @if($warehouses)
                @foreach($warehouses as $v => $warehouse)
                    <option value="{{ $warehouse->id }}">{{ $warehouse->name }}</option>
                @endforeach
            @endif
        </select>
    </div>
</div>

jQuery

$('.warehouse').select2({
        placeholder: "Select a Warehouse",
    });
    $(document).on('select2:select', '#warehouse_from', function(e){
        // $('#warehouse_from  ').on('change',function(){
        //send ajax request
        var id         = $(this).val(); 
        var val        = $('#warehouse_from option:selected').text();
        alert(val);
       $(this).find('option').prop('disabled',false);  
       $(this).find('option[value='+val+']').prop('disabled',true);
        getItemsByWarehouse(id);
        e.preventDefault();
        return false;
    });

问题:为什么我的残疾人没有工作?

2 个答案:

答案 0 :(得分:0)

您得到的是文本,而不是所选选项元素的值:

var val = $('#warehouse_from option:selected').text();
// ...
$(this).find('option[value='+val+']').prop('disabled',true);

应该是:

var val = $('#warehouse_from option:selected').val();
// ...
$(this).find('option[value='+val+']').prop('disabled',true);

答案 1 :(得分:0)

我的错误是禁用来自Warehouse_from的值,而不是不使用Warehouse_to。

**Jquery**

$(document).on('select2:select', '#warehouse_from', function(e){
        //send ajax request
        var id         = $(this).val(); 
        var val        = $('#warehouse_from option:selected').text();
        alert(id);
       $('select#warehouse_to>option[value="'+id+'"]').attr('disabled','disabled');
        getItemsByWarehouse(id);
        e.preventDefault();
        return false;
    });