【发布时间】:2016-03-24 18:23:07
【问题描述】:
我有 4 个下拉选择,默认情况下,下拉列表中的所有值都被禁用,除了第一个。在第一个下拉列表中选择的值确定将启用其他 3 个值中的哪一个。这意味着在 3 个下拉列表中,预计只会选择一个值。代码按预期禁用和启用,但是在这 3 个值中,我无法获取所选元素的值。我该如何帮忙。
var selectedStoredValue = "";
$(document).ready(function() {
$('select#goodopt').find('option').each(function() {
$(this).prop('disabled', true);
});
$('select#effectiveopt').find('option').each(function() {
$(this).prop('disabled', true);
});
$('select#socialopt').find('option').each(function() {
$(this).prop('disabled', true);
});
$("#producttype").change(function() {
if ($("#producttype").prop('selectedIndex') == "1") {
$('select#goodopt').find('option').each(function() {
$(this).prop('disabled', false);
});
} else {
$('select#goodopt').find('option').each(function() {
$(this).prop('disabled', true);
selectedStoredValue = "";
});
}
});
$("#producttype").change(function() {
if ($("#producttype").prop('selectedIndex') == "2") {
$('select#effectiveopt').find('option').each(function() {
$(this).prop('disabled', false);
selectedStoredValue = $("#effectiveopt option:selected").text();
});
} else {
$('select#effectiveopt').find('option').each(function() {
$('#effectiveopt').val('');
$(this).prop('disabled', true);
selectedStoredValue = "";
});
}
});
$("#producttype").change(function() {
if ($("#producttype").prop('selectedIndex') == "3") {
$('select#socialopt').find('option').each(function() {
$(this).prop('disabled', false);
selectedStoredValue = $("#socialopt option:selected").text();
});
} else {
$('select#socialopt').find('option').each(function() {
$('#socialopt').val('');
$(this).prop('disabled', true);
selectedStoredValue = "";
});
}
});
console.log(selectedStoredValue);
});
【问题讨论】:
-
禁用选择,而不是选项,您应该首先通过 html 属性:
disabled="disabled"。然后,当您的选择发生变化时,您可以比较第一个选择的值并相应地启用/禁用其他选择。 -
感谢回复,能否提供一个简单的sn-p来实现这个?
标签: javascript jquery drop-down-menu html-select selectedvalue