首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何禁用已在另一个<select>中选择的选项?

如何禁用已在另一个<select>中选择的选项?
EN

Stack Overflow用户
提问于 2013-07-23 01:47:54
回答 4查看 3.2K关注 0票数 4

我有五个HTML值不同的selects,但是选项具有相同的文本。如何比较文本选项而不是值,并在每隔一个select中禁用相应的选项?例如,如果我有:

代码语言:javascript
复制
<select name="options[70]" id="select_70">
  <option value="1" price="0"> test-1 </option>
  <option value="2" price="0"> test-2 </option>
  <option value="3" price="0"> test-3 </option>
</select>
<select name="options[71]" id="select_71">
  <option value="4" price="0"> test-1 </option>
  <option value="5" price="0"> test-2 </option>
  <option value="6" price="0"> test-3 </option>
</select>
<select name="options[72]" id="select_72">
  <option value="7" price="0"> test-1 </option>
  <option value="8" price="0"> test-2 </option>
  <option value="9" price="0"> test-3 </option>
</select>
<select name="options[73]" id="select_73">
  <option value="10" price="0"> test-1 </option>
  <option value="11" price="0"> test-2 </option>
  <option value="12" price="0"> test-3 </option>
</select>
<select name="options[74]" id="select_74">
  <option value="13" price="0"> test-1 </option>
  <option value="14" price="0"> test-2 </option>
  <option value="15" price="0"> test-3 </option>
</select>

假设用户在最后一个select中选择了选项test-1。这应该会在其他选择中禁用相应的option。我怎样才能做到这一点呢?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-07-23 01:59:51

如果我理解正确的话,可能是这样的:

代码语言:javascript
复制
var $selects = $('select');
$selects.on('change', function() {
    var $select = $(this), 
        $options = $selects.not($select).find('option'),
        selectedText = $select.children('option:selected').text();

    var $optionsToDisable = $options.filter(function() {
        return $(this).text() == selectedText;
    });

    $optionsToDisable.prop('disabled', true);
});

//to apply initial selection
$selects.eq(0).trigger('change');

jsFiddle:example

票数 8
EN

Stack Overflow用户

发布于 2013-07-23 02:26:57

在@IgorDymov的回答(功劳应该归功于他)的基础上,我可能会这么做(参见fiddle):

代码语言:javascript
复制
var $selects = $('select');
$selects.on('change', function() {
    $("option", $selects).prop("disabled", false);
    $selects.each(function() {
        var $select = $(this), 
            $options = $selects.not($select).find('option'),
            selectedText = $select.children('option:selected').text();
        $options.each(function() {
            if($(this).text() == selectedText) $(this).prop("disabled", true);
        });
    });
});

$selects.eq(0).trigger('change');

这样,每次创建新的属性时,我们都会完全刷新选择,这意味着属性将再次重新启用。你会注意到在小提琴中我减少了选择的数量,增加了选项的数量,这样你就不会陷入所有选项同时被禁用的情况,我认为这是最有意义的。

票数 3
EN

Stack Overflow用户

发布于 2013-07-23 02:22:51

一个长度示例

代码语言:javascript
复制
var $selects = $('select');
available = {};

// Get the text of options from the dropdown
// and store in a object hash 
$('option', $selects.eq(0)).each(function (i) {
    var val = $.trim($(this).text());
    available[val] = false;
});

$selects.change(function () {
    var $this = $(this);
    var selectedVal = $.trim($this.find('option:selected').text());

    // set that value to true
    available[selectedVal] = true;

    $selects.not(this).each(function () {
        $('option', this).each(function () {
            var optText = $.trim($(this).text()),
                optCheck = available[optText];

            $(this).prop('disabled', optCheck );
        });
    });
}).change(); // Trigger once to add options at load of first choice
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17794064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档