【问题标题】:Dynamically select Drop Down in Jquery在Jquery中动态选择下拉
【发布时间】:2017-01-17 11:42:56
【问题描述】:

我有 4 个下拉菜单。

默认情况下,每个 drop 都有一个 --select-- 选项。每个盒子都有一个唯一的 id。如您所见,如果上面的下拉值为 --select--,则禁用第二个下拉列表。它只会在值不是 --select--

时启用

这是我的代码:

$(document).ready(function() {

$('#idOfTheFirstDropDown').bind('change', function() {
    var options = $(this).children('option');
    var item = $('div.c-select').children().hide(); // hide all the elements
    var value = Math.floor(Math.random() * options.length );
    //console.log(value);
    if (value.length) { // if selected 
        item.show(); // show the next dropdown
    }
}).trigger('change');
});

我希望它仅在我之前的下拉列表值为 not --select-- 时才显示下一个下拉列表。我的代码正在接受第一个下拉菜单,但没有更改值。我究竟做错了什么?谢谢。

一个下拉框的我的 HTML。剩下的 3 只更改了 ID。HTML 的其余部分保持不变。

<div class="c-select">
<select name="list1" onchange="javascript:setTimeout('__doPostBack(\'list1\',\'\')', 0)" id="idOfTheFirstDropDown" class="GroupDD dropDown ">
<option selected="selected" value="">-- Select --</option>
<option value="1">Group1</option>
</select>
</div>

【问题讨论】:

  • 如果你能提供 HTML 会更容易。
  • @HubertS:谢谢你的建议。我为第一个下拉菜单添加了 HTML。

标签: javascript jquery validation


【解决方案1】:

我不会隐藏选项,而是使用禁用属性(代码中的 cmets):

var selects = $('.drop-down');
selects.not(':eq(0)').prop('disabled', true); // disable all but first drop down

selects.on('change', function() {
  var select = $(this),
      currentIndex = selects.index(select),
      nextIndex = currentIndex + 1;
  
  // only do this if it is not last select
  if (currentIndex != selects.length - 1) { 
    selects.slice(nextIndex)        // get all selects after current one
           .val('')                 // reset value
           .prop('disabled', true); // disable 
    
    selects.eq(nextIndex).prop('disabled', select.val() === ''); // disable / enable next select based on val
  }
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="drop-down">
  <option value="">--Select--</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
</select><br>
<select class="drop-down">
  <option value="">--Select--</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
</select><br>
<select class="drop-down">
  <option value="">--Select--</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
</select><br>
<select class="drop-down">
  <option value="">--Select--</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
</select>

【讨论】:

  • 感谢您的回复。出于某种原因,它对我不起作用。我在我的问题中更新了我的 HTML。你能从中看出点什么吗?
  • 您的所有选择都必须具有相同的类,您应该在选择器中使用它。另外,您使用的是什么版本的 jQuery,因为您似乎正在使用已弃用的函数 (.bind)
  • 它们具有相同的类(.c-select)。另外,我需要自动化代码。就像在控制台中按回车一样,它应该自动采用除选择之外的值。我错过了一条线吗?如果是,您能否相应地更新您的代码?谢谢!
【解决方案2】:

检查这个;

HTML 代码:

<select id="sel-1"></select>
<select id="sel-2"></select>
<select id="sel-3"></select>
<select id="sel-4"></select>

JS代码:

$(document).ready(function(){

     $("[id^=sel]").change(function(){

           /*
           *  find out the current box id
           */

           var id=$(this).attr("id");
           /*
           *  find out the current box id order number
           */

           var ordernumber=id.split("-")[1];

           if($(this).val()!="select")
           {
                 //enable next one
                 $("#sel-"+(ordernumber+1)).show();
           }
     })
})

【讨论】:

  • 感谢您的回复。你能解释一下你的cmets吗?如何找到当前 ID?
  • var id=$(this).attr("id"); - 这将为您提供当前 ID
【解决方案3】:

假设具有'--select--' 作为文本的选项没有值,只需在处理程序上使用val 来检查所选选项是否不是空的

if(this.val() !== '') {
// enable following select
}

【讨论】:

    【解决方案4】:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <style>
      select {
        margin: 5px 0;
        display: block;
      }
    </style>
    <script>
      $(function() {
        $('.cls-select').change(function() {
    
          if ($(this).val() == '--select--') {
    
            // reset and disable the next drop downs
            $('.cls-select:gt(' + $('.cls-select').index($(this)) + ')').val('--select--').prop('disabled', true)
    
          } else {
    
            // current drop down has value so enable the next drop down
            $(this).next().prop('disabled', false);
          }
        });
      })
    </script>
    
    <select class="cls-select">
      <option>--select--</option>
      <option>one</option>
      <option>two</option>
    </select>
    <select class="cls-select" disabled>
      <option>--select--</option>
      <option>one</option>
      <option>two</option>
    </select>
    <select class="cls-select" disabled>
      <option>--select--</option>
      <option>one</option>
      <option>two</option>
    </select>
    <select class="cls-select" disabled>
      <option>--select--</option>
      <option>one</option>
      <option>two</option>
    </select>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 2013-03-29
      • 1970-01-01
      • 2020-01-06
      • 2021-12-06
      • 1970-01-01
      相关资源
      最近更新 更多