如何根据其他选择的选项禁用选择? [英] How to disable select based on other selected option?

查看:60
本文介绍了如何根据其他选择的选项禁用选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的jQuery函数有问题。我希望基于一个选定的选项来禁用其他选择。在我的情况下,如果选择了选项1中的选项1,我想禁用select2。所以我使用这个jQuery函数:

I have a problem with jQuery function. I want based on one selected option to disable other select. In my case, I want to disable select2 if option 1 from select 1 is chosen. So i use this jQuery function:

$(document).ready(function(e) {
$('.select1').change(function(e) {
    if ($('.select1').val()==1){
        $('.select2').attr('disabled','disabled');
    }
    else{
        $('.select2').removeAttr('disabled');
    }
})
});

但这只适用于我首先选择选项2(或此选择的任何其他选项)然后重新选择选项1(我想这是因为使用了更改功能)。如何禁用select2当默认select1设置为选项1 ???

But this works only if I first select option 2(or any other option form this select) and then reselect option 1(i suppose that is because use of change function). How to disable select2 when by the default select1 is set to option 1???

$(document).ready(function(e) {
    if ($('.select1').val()==1){
    $('.select2').attr('disabled','disabled');
}
else{
    $('.select2').removeAttr('disabled');
}
})

这也不会起作用:/

推荐答案

它听起来你想在启动时触发'更改'事件。

It sounds like you want to trigger a 'change' event at startup.

$('.select1').trigger('change');

所以,

$(document).ready(function(e) {
    $('.select1').change(function(e) {
        if ($('.select1').val()==1){
            $('.select2').attr('disabled','disabled');
        }
        else{
            $('.select2').removeAttr('disabled');
        }
    });
    $('.select1').trigger('change');
});

这篇关于如何根据其他选择的选项禁用选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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