Question

How to select ubuntu from the following drop down box

<select><option value=""></option>
   <option value="" selected="selected">Select Image</option>
   <option value="3f2cb990-cd0e-4bfa-a23e-54701498a2e5" data-volume_size="2">ubuntu-12.04-server-cloudimg-amd64 (1.4 GB)</option>
</select>

I tried the following still this doesnt work

$('#id_image_id').find('option:contains("Ubuntu"):contains("12.04")').prop('selected',true) || $('#id_image_id').find('option:contains("ubuntu"):contains("12.04")').prop('selected',true);
Était-ce utile?

La solution

Try this:

$("select").val("3f2cb990-cd0e-4bfa-a23e-54701498a2e5");

or

$('select option[value="3f2cb990-cd0e-4bfa-a23e-54701498a2e5"]').attr('selected','selected');

To get By text: that contains ubuntu

 $('select').find('option:contains("ubuntu")').prop('selected', true);

To get that contents ubuntu as well as 12.04

 $('select').find('option:contains("ubuntu"):contains("12.04")').prop('selected', true);

Working Demo

Autres conseils

Since :contains selector is case-sensitive, you can only use:

$('#id_image_id').find('option:contains("ubuntu"):contains("12.04")').prop('selected', true);

Based on your comment, you need to extend the current method of :contains selector:

$.expr[":"].contains = $.expr.createPseudo(function(arg) {
    return function( elem ) {
        return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
    };
});

Fiddle Demo

Reference

Check this Working Demo jsFiddle

$("select#id_image_id option").filter(":contains('ubuntu')").prop('selected',true);

your mistake is you are not define Id and another things filter contains is strongly case sensitive


Another way also you can done same things,

$('select option[value="3f2cb990-cd0e-4bfa-a23e-54701498a2e5"]').attr('selected','selected');

Check this demo jsFiddle


$(function() {
    $("#valget").val('3f2cb990-cd0e-4bfa-a23e-54701498a2e5');
});

Check this demo jsFiddle

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top