admin管理员组文章数量:1323330
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 /
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/3pfo1d1f/4/
Share edited Oct 19, 2015 at 9:00 Panos Bariamis 4,6637 gold badges39 silver badges62 bronze badges asked Oct 15, 2015 at 6:03 JohnJohn 1,3233 gold badges29 silver badges72 bronze badges2 Answers
Reset to default 4Do it like this: https://jsfiddle/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
});
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
本文标签:
版权声明:本文标题:disable dropdown options based on the previously selected dropdown values using jquery or javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742139953a2422536.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论