简体   繁体   English

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

[英]Select2: Disabled selected option value not working

I have module that you will transfer the qty from "warehouse from" to "warehouse to"(Warehouse From & Warehouse To these are my dropdown) 我有一个模块,您可以将数量从“仓库从”转移到“仓库到”(“仓库从&仓库到我的下拉列表”)

I want put some validation that you cannot longer choose the first selected warehouse coming from "warehouse from" 我想验证一下您不再选择第一个来自“仓库来自”的仓库

View 视图

<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 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;
    });

Question: Why my disabled didnt work? 问题:为什么我的残疾人没有工作?

You getting the text, not the value of the selected option element: 您得到的是文本,而不是所选选项元素的值:

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

It should be: 它应该是:

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

My mistake is im disabling the value coming from warehouse_from not in the warehouse_to. 我的错误是禁用来自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;
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM