[英]How to make the dropdown option disable in HTML web page
I have web page with three dropdown. 我有三个下拉菜单的网页。 It is working fine, except for one problem.
除了一个问题,它工作正常。
When we select any item from one dropdown then it is disabled in the other two dropdowns, but if we again select another item from first dropdown then it disables this item but it also disables the previously selected item. 当我们从一个下拉列表中选择任何项目时,它会在其他两个下拉列表中被禁用,但如果我们再次从第一个下拉列表中选择另一个项目,则会禁用此项目,但它也会禁用之前选择的项目。
The previously selected should be enabled when new option is disabled. 禁用新选项时,应启用先前选择的选项。
Here is the code: 这是代码:
<select id="select1" name="indication_subject[]">
<option value="" selected="selected">a </option>
<option value="1"> Accounting</option>
<option value="2"> Afrikaans</option>
<option value="3"> Applied Information and Communication Technology</option>
<option value="4"> Arabic</option>
<option value="5"> Art and Design</option>
<option value="6"> Biology</option>
<option value="7"> Business Studies</option>
</select>
<select id="select2" name="indication_subject[]">
<option value="" selected="selected">a </option>
<option value="1"> Accounting</option>
<option value="2"> Afrikaans</option>
<option value="3"> Applied Information and Communication Technology</option>
<option value="4"> Arabic</option>
<option value="5"> Art and Design</option>
<option value="6"> Biology</option>
<option value="7"> Business Studies</option>
</select>
<select id="select3" name="indication_subject[]">
<option value="" selected="selected">a </option>
<option value="1"> Accounting</option>
<option value="2"> Afrikaans</option>
<option value="3"> Applied Information and Communication Technology</option>
<option value="4"> Arabic</option>
<option value="5"> Art and Design</option>
<option value="6"> Biology</option>
<option value="7"> Business Studies</option>
</select>
Here is the JavaScript code: 这是JavaScript代码:
$(document).ready(function(){
$("select").change(function() {
$("select").not(this).find("option[value="+ $(this).val() + "]").attr('disabled', true);
});
});
Demo Link: http://jsfiddle.net/x4E5Q/137/ 演示链接: http : //jsfiddle.net/x4E5Q/137/
Combine the focus event with the change event to achieve what you want: 将焦点事件与更改事件相结合,以实现您的目标:
JavaScript JavaScript的
$(document).ready(function () {
var previous;
$("select").focus(function () {
// Store the current value on focus and on change
previous = this.value;
}).change(function () {
// Do something with the previous value after the change
$("select").not(this).find("option[value=" + previous + "]").prop('disabled', false);
$("select").not(this).find("option[value=" + $(this).val() + "]").prop('disabled', true);
// Make sure the previous value is updated
previous = this.value;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.