简体   繁体   English

如何从选择元素中删除选项

[英]How To Remove Options From select element

I have two dropdown menus.我有两个下拉菜单。 One is enables the user pick a 'time from', The other is to pick 'time until'.一种是允许用户选择“时间从”,另一种是选择“时间直到”。 In order to assure the user does not pick a time on the second dropdown that is before the first time, I want to remove all times from the second dropdown that are earlier then the time picked in the first dropdown {via javascript).为了确保用户不会在第一次之前的第二个下拉列表中选择时间,我想从第二个下拉列表中删除所有时间早于在第一个下拉列表中选择的时间(通过 javascript)。 How can I do this?我怎样才能做到这一点?

<select class="fromWhen form-control" name="From" id="">
    <option value=""></option>
    <option value="12:00:00">12:00 PM</option>
    <option value="12:30:00">12:30 PM</option>
    <option value="13:00:00">1:00 PM</option>
    <option value="13:30:00">1:30 PM</option>
    <option value="14:00:00">2:00 PM</option>
</select>


<select class="untilWhen form-control " name="Until" id="">
    <option value=""></option>
    <option value="12:00:00">12:00 PM</option>
    <option value="12:30:00">12:30 PM</option>
    <option value="13:00:00">1:00 PM</option>
    <option value="13:30:00">1:30 PM</option>
    <option value="14:00:00">2:00 PM</option>
</select>

You have to create code where you must have to clear all options of second div.您必须创建代码,您必须清除第二个 div 的所有选项。 When he select first option (time from) then show time until option and make condition if (time until) is less than (time from) then remove that div.当他选择第一个选项 (time from) 然后显示 time until 选项并设置条件 if (time until) 小于 (time from) 然后删除该 div。

Check the working of code there ... https://jsfiddle.net/Arsh_kalsi01/Lr8uxzs5/检查那里的代码工作...... https://jsfiddle.net/Arsh_kalsi01/Lr8uxzs5/

<script
              src="https://code.jquery.com/jquery-3.1.1.min.js"
              integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
              crossorigin="anonymous"></script>

<select class="fromWhen form-control" name="From" id="">
    <option value="0">Select Time From</option>
    <option value="12:00:00">12:00 PM</option>
    <option value="12:30:00">12:30 PM</option>
    <option value="13:00:00">1:00 PM</option>
    <option value="13:30:00">1:30 PM</option>
    <option value="14:00:00">2:00 PM</option>
</select>


<select class="untilWhen form-control " name="Until" id="">
    <option value="0">Select Time Until</option>
    <option value="12:00:00">12:00 PM</option>
    <option value="12:30:00">12:30 PM</option>
    <option value="13:00:00">1:00 PM</option>
    <option value="13:30:00">1:30 PM</option>
    <option value="14:00:00">2:00 PM</option>
</select>





<script>
$(document).ready(function(){
    var data = $(".untilWhen").html();
  $(".untilWhen").html("");

  $(".fromWhen").on('change',function(){
    var fromval = $(this).val();
    $(".untilWhen").html(data);
    $(".untilWhen").find("option").each(function(){
    var obj = $(this);
        if(obj.val()<=fromval)
      {
obj.remove();
            }
    });

  });

});
</script>
  1. Use onChange event on first dropdown.在第一个下拉列表中使用 onChange 事件。
  2. Go through this QA- Change event on select dropdown . 在 select dropdown 上完成这个 QA- Change 事件

Use .remove() when you want to remove the element itself, as well as everything inside it.当您想要删除元素本身以及其中的所有内容时,请使用 .remove() 。 In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.除了元素本身之外,所有与元素关联的绑定事件和 jQuery 数据都将被删除。

To remove the elements without removing data and events, use .detach() instead.要在不删除数据和事件的情况下删除元素,请改用 .detach() 。

Example:例子:

$("option::nth-child(1)").remove();

Or in your case:或者在你的情况下:

$(".untilWhen > option").remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM