Jquery - 三个选择列表,如果选择了一个值,则停用另外两个

发布于 2024-09-14 03:47:05 字数 106 浏览 0 评论 0原文

我有三个下拉选择列表,当选择其中一个列表中的值时,我希望其他两个显示为灰色且处于非活动状态。我在我正在试验的每个选择列表周围都有 id 包装器,但由于我是 jquery 新手,所以我并没有走得太远。

I have three drop down select lists, when a value from one of the list is selected, I want the other two to be greyed out and inactive. I have id wrappers around each select list that I'm experimenting on, but as I am new with jquery I'm not getting very far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木槿暧夏七纪年 2024-09-21 03:47:05

像这样的东西应该有效:

$(function() {
  var $selects = $('#select1, #select2, #select3');
  $selects.change(function() {
    // disabled the other two
    $selects.not(this).attr('disabled', 'disabled');
  });
});


Updated version:

$(function() {
  var $selects = $('#select1, #select2, #select3');
  $selects.change(function() {
    // disable or enable the other two
    $selects.not(this).attr('disabled', $(this).val() === '' ? '' : 'disabled');
  });
});

Something like this should work:

$(function() {
  var $selects = $('#select1, #select2, #select3');
  $selects.change(function() {
    // disabled the other two
    $selects.not(this).attr('disabled', 'disabled');
  });
});


Updated version:

$(function() {
  var $selects = $('#select1, #select2, #select3');
  $selects.change(function() {
    // disable or enable the other two
    $selects.not(this).attr('disabled', $(this).val() === '' ? '' : 'disabled');
  });
});
神魇的王 2024-09-21 03:47:05

我假设您有三个,如果选择了三个中的任何一个,则其他两个应该禁用。

function toggleOthers(x) {
var select = new Array('#wrapper1','#wrapper2','#wrapper3');

for (int i = 0; i < select.length; i++ )
    if ($(x).attr('id')!=select[i])
        $(x).attr('disabled','disable');
}

HTML:

I assume you have three, and if any of the three are selected, the other two should disable.

function toggleOthers(x) {
var select = new Array('#wrapper1','#wrapper2','#wrapper3');

for (int i = 0; i < select.length; i++ )
    if ($(x).attr('id')!=select[i])
        $(x).attr('disabled','disable');
}

HTML:
<select onchange='toggleOthers(this);' id='wrapper1'> etc...

茶花眉 2024-09-21 03:47:05

您可以执行以下操作:

$('#wrappers').change(function(){
  $('#select1').attr('disabled', true);
  $('#select2').attr('disabled', true);
});

其中 wrappers 是 select 元素的 id,当您从中选择一个值时,另外两个 id 为 select1select2 的元素将被禁用 分别例如:

<select id="wrappers">.........
<select id="select1">.........
<select id="select2">.........

这是工作示例

You can do something like this:

$('#wrappers').change(function(){
  $('#select1').attr('disabled', true);
  $('#select2').attr('disabled', true);
});

Where wrappers is the id of select element from which when you choose a value the other two get disabled whose id is select1 and select2 respectively eg:

<select id="wrappers">.........
<select id="select1">.........
<select id="select2">.........

Here is working example

两个我 2024-09-21 03:47:05

如果您有带有 id 为 #wrapper
的 div 包装器的选择

$('#wrapper select').each(function(i,select){
   $(select).change(function(){
        $(this).attr('class','enabled').siblings().attr('class','disabled');
    })
})

if you have the selects with a div wrapper with an id of #wrapper

$('#wrapper select').each(function(i,select){
   $(select).change(function(){
        $(this).attr('class','enabled').siblings().attr('class','disabled');
    })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文