选中时更改选项

时间:2017-04-14 06:57:18

标签: javascript jquery

我有预订表格。如果我的一个客户遇到问题,我希望他们在他们喜欢的时间给我打电话。为此,我创建了两个下拉列表框。第一个组合框应包含两个选项(如代码段中所示),默认情况下选择立即调用。如果选择第二个选项立即呼叫,则第二个下拉列表应填充可用的时间段,如:

12:00
13:00
16:00
20:00
...
...

等等。怎么可以实现呢?

  
  $("#call-day").change(function(e){
    val=$("option:selected",this).val();
    if(val=="today"){
      
    }else if(val=="tomorrow"){
      
    }
  });
<select name="call-day" id="when-ajax">
  <option class="today">Call Today</option>
  <option class="tomorrow">Call Tomorrow</option>
</select>

<select name="hours">
  <option>Call Now</option>
</select>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1 个答案:

答案 0 :(得分:1)

你的问题太模糊了。请改写它。根据我的描述,您的描述可以帮助您实现您的目标。请注意,我冒昧地承担了某些事实。

&#13;
&#13;
  
  $("select[name='call-day']").change(function(e){
    val=$(this).val();
    if(val=="today"){
      //What you want to do here is not mentioned. So I am leaving it blank
    }else if(val=="tomorrow"){
      //Available time slots should be retrieved from some persistent location. Assuming it is already done...
      var availableSlots = ["12:00", "13:00", "14:00", "16:00"];
      $("select[name='hours']").empty();
      $("select[name='hours']").append("<option value=''>--Select Time--</option");
      for(i in availableSlots){
        var item = availableSlots[i];
        var optionElement = "<option value='"+ item +"'>" + item + "</option>";
        $("select[name='hours']").append(optionElement);
      }
    }
  });
&#13;
<select name="call-day" id="when-ajax">
  <option value="today">Call Today</option>
  <option value="tomorrow">Call Tomorrow</option>
</select>

<select name="hours">
  <option>Call Now</option>
</select>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;