javascript - 从禁用和启用下拉列表中获取选定的值

标签 javascript jquery drop-down-menu html-select selectedvalue

我有 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:

<select id="select_1">
    <option value="enable_select_2">Enable 2</option>
    <option value="enable_select_3">Enable 3</option>
</select>

<select id="select_2" class="sub-select" disabled="disabled">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

<select id="select_3" class="sub-select" disabled="disabled">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

示例 JS:

$('#select_1').change(function() {
    // First disable all selects that have the class "sub-select"
    $('select.sub-select').disable();

    // Now enable the correct select
    if ($(this).val() == 'enable_select_1') {
        $('#select_1').enable();
    } else if ($(this).val() == 'enable_select_2') {
        $('#select_2').enable();
    }
});

关于javascript - 从禁用和启用下拉列表中获取选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36207384/

相关文章:

javascript - 是否可以将唯一的 CSS 类附加到多个 <li> 元素,或使用 Jquery 对它们重新排序?

jquery - 功能 Carousel js 的响应式设计

css - 如何在 Bootstrap 3 的容器外创建全宽下拉菜单?

javascript - Accordion - 单击时自动切换展开/折叠箭头未正确调整

php - 数据库查询信息到下拉菜单

javascript - 有什么方法可以使用 velocity js 将渐变颜色应用于文本?

javascript - jQuery 阻止 onmouseover 事件

jquery - jquery选择器的区别

javascript - 选择元素关闭时向选择元素添加类

javascript - PHP Ajax mysql 选择 onload