disable dropdown options based on the previously selected dropdown values using jquery or javascript?

不打扰是莪最后的温柔 提交于 2019-12-04 21:16:31
Sandeep Nayak

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!