如何使用jquery启用/禁用“选择框"中的选项 [英] How do I enable/disable options in Select Box using jquery

查看:62
本文介绍了如何使用jquery启用/禁用“选择框"中的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择框,其中包含使用php动态生成的选项和optgroup.现在,当我将所有其他optgroup全部选择为全部"时,应该禁用选项,而当我选择除"ALL"应禁用"ALL"选项

I have a select box which contains the options and optgroup that are generated dynamically using php.Now when I select "ALL" all the other optgroup, options should be disabled and when I select any option other than "ALL" the "ALL" option should be disabled

<select name="select1" id ="select1" onchange="handleSelect()">
    <option value ="-1">ALL</option>
    <optgroup label="CARS">
        <option value="ford">FORD</option>
        <option value="Nissan">Nissan</option>
    </optgroup>
</select>
<script>
    function handleSelect() {
        var selectVal = $("#select1:selected").text();
        if (selectVal == "ALL") {
            // cannot disable all the options in select box
            $("#select1  option").attr("disabled", "disabled");
        }
        else {
            $("#select1 option[value='-1']").attr('disabled', 'disabled');
            $("#select1 option").attr('disabled', '');
        }
    }
</script>

如何使它正常工作?

推荐答案

这是一件很奇怪的事情,但是这里的代码可以满足您的要求.

This is kind of a strange thing to be doing but here's code that meets your requirements.

$('select').on('change', function() {
    if (this.value == '-1') {
        $('optgroup option').prop('disabled', true);
    } else {
        $('optgroup option').prop('disabled', false);
    }
});

实时示例- http://jsfiddle.net/NpNFh/

这篇关于如何使用jquery启用/禁用“选择框"中的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆