开发者

Set the default value in dropdownlist using jQuery

开发者 https://www.devze.com 2023-02-06 15:24 出处:网络
I have many options in my dropdownlist like: <option value=\"1\">it\'s me</option> I need to select the option who have value it\'s m开发者_开发问答e inside the tag, not by attribute li

I have many options in my dropdownlist like:

<option value="1">it's me</option>

I need to select the option who have value it's m开发者_开发问答e inside the tag, not by attribute like 1.

How can I do this using jQuery?


if your wanting to use jQuery for this, try the following code.

$('select option[value="1"]').attr("selected",true);

Updated:

Following a comment from Vivek, correctly pointed out steven spielberg wanted to select the option via its Text value.

Here below is the updated code.

$('select option:contains("it\'s me")').prop('selected',true);

You need to use the :contains(text) selector to find via the containing text.

Also jQuery prop offeres better support for Internet Explorer when getting and setting attributes.

A working example on JSFiddle


You can just do this:

$('#myCombobox').val(1)


val() should handle both cases

  <option value="1">it's me</option>      


$('select').val('1'); // selects "it's me"

$('select').val("it's me"); // also selects "it's me"


$('#userZipFiles option').prop('selected', function() {
        return this.defaultSelected;
    });     


$("#dropdownList option[text='it\'s me']").attr("selected","selected"); 


jQuery("select#cboDays option[value='Wednesday']").attr("selected", "selected");


One line of jQuery does it all!

$("#myCombobox option[text='it\'s me']").attr("selected","selected"); 


This is working fine:

$('#country').val($("#country option:contains('It\'s Me')").val());
0

精彩评论

暂无评论...
验证码 换一张
取 消