disable dropdown options based on the previously s

2019-04-17 14:13发布

问题:

I have three dropdowns with the same options. What i want is, when i selecte an option from a dropdown, the same option from the other two dropdowns to be disabled, except from the first one (the option 'Select One').

For this i am using the following logic, but here when one dropdown is selected to one value instead of disabling that selected value from the remaining two dropdowns, its disabling from the all of these three dropdowns this i don't want. The disabling of value should happen to the remaining ones but not for the current dropdown.

How can i do this?

<select class="form-control positionTypes">
    <option value="">Select One</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
</select>

<select class="form-control positionTypes">
    <option value="">Select One</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
</select>

<select class="form-control positionTypes">
    <option value="">Select One</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
</select>
$("select.positionTypes").change(function () {
    $("select.positionTypes option[value='" + $(this).data('index') + "']").prop('disabled', false);
    $(this).data('index', this.value);
    $("select.positionTypes option[value='" + this.value + "']:not([value=''])").prop('disabled', true);
});

here is the fiddle link https://jsfiddle.net/3pfo1d1f/4/

回答1:

Do it like this: https://jsfiddle.net/sandenay/3pfo1d1f/7/

$("select.positionTypes").change(function () {
    $("select.positionTypes option[value='" + $(this).data('index') + "']").prop('disabled', false); //reset others on change everytime
    $(this).data('index', this.value);
    $("select.positionTypes option[value='" + this.value + "']:not([value=''])").prop('disabled', true);
    $(this).find("option[value='" + this.value + "']:not([value=''])").prop('disabled', false); // Do not apply the logic to the current one
});


回答2:

There is a simple way of doing this. you can achieve the same with simple logic and its easily understandable even for a beginner as well.

How it works:

  • Reset all the previous selection made on other dropdowns
  • Disable the selected option on all dropdowns excluding current dropdown.

JQUERY CODE:

$(function () {
    $(".positionTypes").on('change', function () {
        var selectedValue = $(this).val();
        var otherDropdowns = $('.positionTypes').not(this);
        otherDropdowns.find('option').prop('disabled', false); //reset all previous selection
        otherDropdowns.find('option[value=' + selectedValue + ']').prop('disabled', true);
    });
});

Live Demo @ JSFiddle