简体   繁体   English

从选择列表中删除已附加到Jquery中的项目

[英]Remove items from select list which have been appended in Jquery

My form allows users to select a vehicle type, which then, using Jquery, appends those values which are checked, to my select list. 我的表单允许用户选择一种车辆类型,然后使用Jquery将选中的那些值附加到我的选择列表中。

All good, except I need to handle the de-selection of vehicles, which should remove the value in the select list. 一切都很好,除了我需要处理车辆的取消选择,这应该删除选择列表中的值。

You should be able to see that on append, I attribute a class of "select_option" which is what I use to try and remove this from the select. 您应该能够看到,在附加项上,我为“ select_option”类指定了属性,这是我用来尝试将其从select中删除的类。 However, it isn't working. 但是,它不起作用。

How can I remove those that are de-selected? 如何删除未选中的内容?

HTML HTML

<form action="">
<input type="checkbox" name="vehicles" data-name="Bike" value="Bike">Bike
<input type="checkbox" name="vehicles" data-name="Car" value="Car">Car
</form>

<select name="choose_car" id="choose_car">
     <option value=""class="select_option">Choose car</option>
</select>

JQUERY JQUERY

$('.vehicles').click(function(){

    $('#choose_car').append('<option value=' + $(this).val() + ' class=\'select_option\'>' + $(this).data("name")) + '</option>';

    $.each($(".vehicles:not(:checked"), function(){
        $('option.select_option').remove();
    });
});

You need to use the values of the non-checked vehicles so you only remove those options: 您需要使用未经检查的车辆的值,因此仅删除这些选项:

$(".vehicles:not(:checked)").each(function() {
    var val = $(this).val();
    $('#choose_car option.select_option[value=' + val + ']').remove();
});

Complete working example (add and remove in the same function) : 完整的工作示例(在同一功能中添加和删除):

each time you check or uncheck, it adds or remove the item from the list 每次您选中或取消选中它都会在列表中添加或删除该项目

you can drop the select_option class 您可以删除select_option

http://jsfiddle.net/dgp4bv3j/1/ http://jsfiddle.net/dgp4bv3j/1/

 $(function() { $("[type='checkbox']").on("change", function() { if($(this).is(":checked")) { $("#choose_vehicle").append("<option value=\\"" + $(this).val() + "\\">" + $(this).val() + "</option>"); } else { $("#choose_vehicle option[value='" + $(this).val() + "']").remove(); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action=""> <input type="checkbox" name="vehicles" data-name="Bike" value="Bike">Bike <input type="checkbox" name="vehicles" data-name="Car" value="Car">Car </form> <select name="choose_vehicle" id="choose_vehicle"> <option value="">Choose vehicle</option> </select> 

What about this? 那这个呢? http://jsfiddle.net/0hk8rLcj/ http://jsfiddle.net/0hk8rLcj/

$('[name=vehicles]').click(function(){


    $('#choose_car').append('<option value=' + $(this).val() + ' class=\'select_option\'>' + $(this).data("name")) + '</option>';

    $("[name=vehicles]:not(:checked)").each(function() {
    var val = $(this).val();
    $('#choose_car option.select_option[value=' + val + ']').remove();
});
});

I completed a similar answer to the others: http://jsfiddle.net/h7w8x7sh/15/ 我完成了对其他人的类似回答: http : //jsfiddle.net/h7w8x7sh/15/

$('.vehicles').click(function(){
var chooseCar = $('#choose_car');

if ($(this).prop('checked')) { 
    chooseCar.append('<option value=' + $(this).val() + ' class=\'select_option\'>' + $(this).data("name") + '</option>');
}
else {
    var ogValue = $(this).val();
    $('#choose_car option').each(function()  {
        var value = $(this).val();
        if (ogValue == value) {
            $(this).remove();
        }
    });
}
});

does the same thing pretty much but I am including it because I spent some time writing it up. 几乎可以做同样的事情,但是我之所以加入它是因为我花了一些时间来编写它。 But yeah you should change the $('.vehicles') unless they are classes (I changed them to classes in my fiddle). 但是,是的,除非它们是类,否则应更改$('。vehicles')(我将它们更改为小提琴中的类)。

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

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