2017-01-12 3 views
0

ich den unten Dropdown in meinem HTML habengenaue Unter String Match jQuery funktioniert nicht wie erwartet

<select name="sel_id" id="sel_id" multiple="yes"> 
    <option value="100">CLOSING CLERK - FirstName Last Name</option> 
    <option value="101">CLOSING COORDINATOR - FirstName Last Name</option> 
    <option value="102">CLOSING CLERK SECOND - FirstName Last Name</option> 
    <option value="103">CLOSING CLERK THIRD - FirstName Last Name</option> 
</select> 

ich ein jQuery-Selektor müssen Sie eine Option Element finden (Drop-Down-Option Element des Multi Auswahlbox), die einen genauen Teilstring von "CLOSING CLERK" enthält. Und dann muss ich diese bestimmte option in diesem Dropdown-Feld auswählen. Hier sollte es nur das erste Optionselement finden und auswählen und nicht das dritte und das vierte.

Bitte beachten Sie, dass diese Bestellung für die oben genannten Optionselemente nicht erforderlich ist.

Ich versuchte die unten jQuery, aber kein Glück.

$('#sel_id').filter(function (index) { 
    return $(this).text().split(' -') === "CLOSING CLERK"; 
}).attr('selected', 'selected'); 
+0

nein, ich Optionswerte nicht (wie 100.101 etc ..) verwenden können. Ich muss dies nur über den Optionstext tun. Gibt es irgendeinen Weg?. Ich habe auch versucht .split ('-') [0] .trim() === "" aber immer noch nicht funktioniert. – user3742125

Antwort

1

Der Wähler ist nicht korrekt, müssen Sie filter() auf options verwenden. Die split()-Methode gibt ein Array zurück, sodass Sie mit dem Indexer auf das erste Array-Element zugreifen und es vergleichen können.

Verwenden Sie außerdem .prop(key, value), um die selected-Eigenschaft festzulegen.

jQuery("#sel_id").change(function(event) { 
 
    jQuery(this).find('option').filter(function(index) { 
 
    return $(this).text().split(' - ')[0].trim() === "CLOSING CLERK"; 
 
    }).prop('selected', 'selected'); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select name="sel_id" id="sel_id" multiple="yes"> 
 
    <option value="100">CLOSING CLERK - FirstName Last Name</option> 
 
    <option value="101">CLOSING COORDINATOR - FirstName Last Name</option> 
 
    <option value="102">CLOSING CLERK SECOND - FirstName Last Name</option> 
 
    <option value="103">CLOSING CLERK THIRD - FirstName Last Name</option> 
 
</select>

Fiddle

0

$('#sel_id option').filter(function(index) { 
 
    return $(this).text().indexOf("CLOSING CLERK -") >= 0 ;}) 
 
     .attr('selected', 'selected');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select name="sel_id" id="sel_id" multiple="yes"> 
 

 
<option value="100">CLOSING CLERK - FirstName Last Name</option> 
 

 
<option value="101">CLOSING COORDINATOR - FirstName Last Name</option> 
 

 
<option value="102">CLOSING CLERK SECOND - FirstName Last Name</option> 
 

 
<option value="103">CLOSING CLERK THIRD - FirstName Last Name</option> 
 

 
</select>

Verwandte Themen