2010-11-30 6 views
4

Wie benutze ich jquery, um die opt1 Zeile zu löschen? Wie wäre es, opt2 zu ändern, um ausgewählt zu werden? Beachten Sie, dass die Werte Zufallszahlen sind.Wie verwende ich jquery, um die Option aus der Auswahlbox zu entfernen

 <select name="ShippingMethod" > 
     <option value="8247(random)">Opt2</option> 
     <option value="1939(random)" selected="selected">Opt1</option> 
     </select> 

Antwort

12

Es hängt davon ab, wie Sie es wählen möchten, to remove by text:

$("select[name=ShippingMethod] option").filter(function() { 
    return this.text == "Opt1"; 
}).remove(); 

Or the selected one:

$("select[name=ShippingMethod] option:selected").remove(); 

Or the second one:

$("select[name=ShippingMethod] option:eq(1)").remove(); 

Or the last one:

$("select[name=ShippingMethod] option").filter(function() { 
    return this.text == "Opt2"; 
}).attr("selected", true); 

You can test it here:

$("select[name=ShippingMethod] option:last").remove(); 

Um nur wählen Sie die Option 2 von Text, können Sie den gleichen .filter() Ansatz oben verwenden.

0

Schnell Vermutung als zwei

$('select[name=ShippingMethod]').val('19395ss'); 

Nick Carver in Frage zu stellen scheint, die erste Frage beantwortet zu haben.

Verwandte Themen