2010-03-11 38 views

Respuesta

8

Edición

Dado que la identificación es único en el documento sin necesidad de relacionarlo con el selecto elemento padre. Usted puede hacer simplemente

$("#option1").remove(); 
3

jQuery:

$("#option1").remove(); 

o

$("#select").remove("#option1"); 

Y el método clásico javascript:

var option1 = document.getElementById("option1"); 
document.getElementById("select1").removeChild(option1); 
+0

'$ ("# SELEC.1")' es innecesario. '$ (" # option1 "). remove()' será suficiente ya que se corresponde con 'id'. –

+1

@Marko: Estaba editando eso mientras publicabas tu comentario :-) Dejé el original porque agregar ID a las etiquetas de opción es bastante raro. –

14

Retire por Valor:

$("#select1 option[value=1]").remove(); 

Quitar por Texto:

$("#select1 option:contains(Text)").remove(); 
+0

Para texto, 'contiene' parece una mala idea, ¿no? ¿Por qué no usar la opción '$ (" # select1: [text = 'My Text'] ")'? –

0

he visto a muchas personas con este problema. He creado este script que podría ser útil. Espero que te guste:

var robable = { 
 
    init: function() { 
 
    robable.get_selected_option_from_select(); 
 
    }, 
 
    get_selected_option_from_select: function() { 
 
    $(".robable").off().on("change", function() { 
 
     if ($(this).val() !== "") { 
 
     robable.add_to_list($(this)); 
 
     } 
 
    }); 
 
    }, 
 
    remove_option_from_select: function(select_id, value) { 
 
    $("#" + select_id + " option[value='" + value + "']").remove(); 
 
    }, 
 
    add_option_to_select: function(select_id, value, text) { 
 
    $('#' + select_id).append('<option value="' + value + '">' + text + '</option>'); 
 
    }, 
 
    add_to_list: function(select) { 
 

 
    option_text = $(".robable option[value='" + select.val() + "']").text(); 
 

 
    //Add to list 
 
    $('#list').append("<li data-value='" + select.val() + "' data-text='" + option_text + "'><a href='#' class='filter-remove' data-parent-id='" + select.attr("id") + "'>Delete</a> " + option_text + "</li>"); 
 
    robable.remove_from_list(); 
 

 
    //Remove from select 
 
    robable.remove_option_from_select(select.attr("id"), select.val()); 
 
    }, 
 
    remove_from_list: function() { 
 
    $(".filter-remove").off().on("click", function() { 
 
     var select_id = $(this).data('parent-id'); 
 
     var option_value = $(this).closest("li").data('value'); 
 
     var option_text = $(this).closest("li").data('text'); 
 

 
     //Add to select 
 
     robable.add_option_to_select(select_id, option_value, option_text); 
 

 
     //Remove from list 
 
     $(this).closest("li").remove(); 
 
    }); 
 
    } 
 
}; 
 

 
robable.init();
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title>Select Robables</title> 
 
</head> 
 

 
<body> 
 
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> 
 

 
    <ul id="list"></ul> 
 

 
</body> 
 

 
</html>

Cuestiones relacionadas