【问题标题】:Semantic UI multi-select dropdown prevent element selection语义 UI 多选下拉菜单阻止元素选择
【发布时间】:2020-04-09 05:50:43
【问题描述】:

我有一个包含 4 个元素的下拉列表,例如

'All', 'Fi', 'Cu', 'Common'. 

这里,只有当其中没有元素时,才允许用户选择'All'。如果其中至少有一个元素说'Cu',并且如果用户选择'All',则必须阻止选择并且'All'将从下拉列表中删除。

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css"></script>

<div style="padding:10px;width:200px;">
    <select id="du_select" class="ui fluid dropdown" name="du_graph_types" multiple="">
        <option value="">Select DU</option>
        <option value="all">All</option>
        <option value="copper">Copper</option>
        <option value="fiber">Fiber</option>
        <option value="common">Common</option>
    </select>
</div>

<script type="text/javascript">
    $('#du_select').dropdown({
        onChange: function(value, text, $selectedItem) {
            // custom action
            if (value.length > 1 && value.includes('all')) {
                const index = value.indexOf('all');
                if (index > -1) {
                    value.splice(index, 1);
                }
                console.log('sel_items : ' + value)
                $('#du_select').dropdown('clear').dropdown('set selected',value)
                alert('Before selecting "All", clear the existing DU type')
            }

        }
    });

</script>

JSFiddle

但是,在这样做的同时,元素会从下拉列表本身中删除。

我在这里犯了什么错误?

【问题讨论】:

    标签: javascript semantic-ui


    【解决方案1】:

    您在逻辑中使用了错误的参数。该解决方案应该可以工作。只需更改您的 onChange 参数即可。

    onChange 下拉菜单为您提供: 1) 已选择的所有选项的数组 2) 当前选择的值

    所以你想这样做:

    $('#du_select').dropdown({
            onChange: function(selected, value) {
        console.log(value)
        console.log(selected)
                // custom action
                if (selected.length > 1 && selected.includes('all')) {
                    const index = selected.indexOf('all');
                    if (index > -1) {
                        selected.splice(index, 1);
                    }
                    console.log('sel_items : ' + selected)
                    $('#du_select').dropdown('clear').dropdown('set selected',value)
                    //$('#du_select').;
                    alert('Before selecting "All", clear the existing DU type')
                }
    
            }
        });
    

    小提琴:https://jsfiddle.net/yumxj47v/

    【讨论】:

    • 这里$('#du_select').dropdown('clear').dropdown('set selected',value) 正在使onChange 函数被多次调用。
    • 当然,但这又是您自己的代码和错误。这个答案解决了您的问题:“元素正在从下拉列表本身中删除。我在这里犯了什么错误?”,这是您的错误参数
    • 在您进行选择时,多次调用函数并不是导致项目从下拉列表中删除的原因。
    【解决方案2】:

    使用 $('#input1 option').length; 获取列表中的选项数量,然后根据您的逻辑应用您的条件。在那里,您将获得 5 的给定代码。让我知道更多细节

    【讨论】:

      【解决方案3】:

      在探索 Semantic-UI 文档后,我找到了解决它的方法,即提供 custom select action。

      <script type="text/javascript">
          $('#du_select').dropdown({
              action: function(text, value) {
                  // nothing built in occurs
                  existing_elems = $(this).dropdown("get value")
                  //console.log(value)
                  if (existing_elems.includes('all')) {
                      alert('No options allowed to add, while "All" is already selected')
                  } else if (existing_elems.length > 0 && value == 'all') {
                      alert('Before selecting "All", clear the existing DU type')
                  } else {
                      $(this).dropdown('set selected',value)
                  }
                  $(this).dropdown('hide')
              }
          });
      
      </script>
      

      JSFiddle

      【讨论】:

      • 您只是在逻辑中使用了错误的参数。检查我的答案,它只会改变你正在使用的参数
      • 另外,你的问题是关于你做错了什么:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多