【问题标题】:Remove a drop down item if it is selected如果选中,则删除下拉项
【发布时间】:2014-10-17 13:54:20
【问题描述】:

我在一个表单上有多个下拉选择实例,确切地说是八个。如果我在第一个选择下拉列表中选择一个数字,我想将第二个选择下拉列表中的所选数字隐藏到第八个。

为此,我将只显示八个选择下拉菜单中的两个。

查看代码 -

选择下拉一个 -

<div class="col-xs-12 col-sm-3">
<div class="form-group">
    <?php echo Form::label('test_id', 'Test', array('class' => 'col-xs-3 control-label')) ?>
    <div class="col-xs-9">
        <select name="test_id" id="test_id" class="form-control select2" data-placeholder="Select...">
            <option value=""></option>                                       
            <?php foreach (ORM::factory('Test')->order_by('id')->find_all() as $row) : ?>
            <option value="<?php echo $row->id ?>" 
            <?php if ($b->id == $row->id) echo 'selected="selected"' ?>>
            <?php echo $row->id ?></option>
            <?php endforeach ?>
       </select>
    </div>
</div>

选择下拉两个-

<div class="col-xs-12 col-sm-3">
<div class="form-group">
    <?php echo Form::label('test_id', 'Test', array('class' => 'col-xs-3 control-label')) ?>
    <div class="col-xs-9">
        <select name="test_id" id="test_id" class="form-control select2" data-placeholder="Select...">
            <option value=""></option>                                       
            <?php foreach (ORM::factory('Test')->order_by('id')->find_all() as $row) : ?>
            <option value="<?php echo $row->id ?>" 
            <?php if ($b->id == $row->id) echo 'selected="selected"' ?>>
            <?php echo $row->id ?></option>
            <?php endforeach ?>
       </select>
    </div>
</div>

jQuery 代码 -

var $selects = $('select');
$('select').change(function () {
    $('option:hidden', $selects).each(function () {
        var self = this,
            toShow = true;
        $selects.not($(this).parent()).each(function () {
            if (self.value == this.value) toShow = false;
        })
        if (toShow) $(this).show();
    });
    if (this.value != "") //to keep default option available
      $selects.not(this).children('option[value=' + this.value + ']').hide();
});

这根本不起作用。

任何帮助将不胜感激。

干杯

【问题讨论】:

  • 你能显示为选择而不是 php.ini 呈现的 html。此外,您发布的两个 php sn-ps 看起来完全相同 - 如果这是正确的,则 id 应该是唯一的
  • $(document).ready() 中的 jQuery 代码吗?
  • 是的,它是 $(document).ready() Vlad
  • 它似乎在工作:jsfiddle.net/10nwqwck/1。我还尝试添加重复的 ID 以查看是否会破坏它,它是否有效。
  • 等一下,你是用 select2 来做选择的吗?

标签: php jquery kohana


【解决方案1】:

这不适用于 select2 和隐藏选项。您可以通过禁用选项和一些 CSS 来隐藏它们来解决这个问题。见下文:

JS

$(document).ready(function () {
    var $selects = $('select');
    $selects.select2();
    $('select').change(function () {
        $('option:hidden', $selects).each(function () {
            var self = this,
                toShow = true;
            $selects.not($(this).parent()).each(function () {
                if (self.value == this.value) toShow = false;
            })
            if (toShow) {
                $(this).removeAttr('disabled');
                $(this).parent().select2();
            }
        });
        if (this.value != "") {
            $selects.not(this).children('option[value=' + this.value + ']').attr('disabled', 'disabled');
            $selects.select2();
        }
    });
})

CSS

.select2-results .select2-disabled {
    display:none;
}

这里的工作示例:http://jsfiddle.net/10nwqwck/4/

【讨论】:

    【解决方案2】:

    这样的事情可能会起作用:

    $(".select").click(function () {
        var value = $(this).val();
    
        // to hide every option that have the selected value
        $("option").filter(function() {    
            return $(this).val() == value;  
        }).hide();
    
        // to show the option currently selected in the current list
        $(this).find("option").each(function () {
            if ($(this).val() == value)
            {
                $(this).show();
            }
        });
    });
    

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:
      $("#Select_id").blur(function () {
              var value = $(this).val();
              alert($("#Select_id").val() == value);
              // to hide every option that have the selected value
              if ($("#Select_id").val() == value) {
                  $("#Option_id"+value).hide();
              }
          });
      

      试试这个 您可以像这样简单地从下拉菜单中删除所选项目。下拉 ID 为“Select_id”,选择的选项 ID 需要为“Option_id”。如果两者都没有,则选择的选项将被删除

      【讨论】:

      • 有一些解释和答案有助于读者理解它的工作原理。
      【解决方案4】:

      干杯弗拉德,工作的伙伴。

      为了让 jQuery 更加优化,这样做了。

      $(document).ready(function () {
        var $selects = $('select');
        $selects.select2();
        $('select').change(function () {
           $('option:hidden', $selects).each(function () {
              var self = this,
                      toShow = true;
              $selects.not($(this).parent()).each(function () {
                 if (self.value == this.value) toShow = false;
              })
              if (toShow) {
                 $(this).removeAttr('disabled');
              }
           });
           if (this.value != "") {
              $selects.not(this).children('option[value=' + this.value + ']').attr('disabled', 'disabled');
           }
        });
      })
      

      感谢您的帮助:)

      【讨论】:

      • 不客气。如果它解决了您的问题,请接受答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 2014-08-28
      • 2014-05-26
      • 2017-12-05
      • 2013-12-04
      相关资源
      最近更新 更多