无法使用jQuery更改选择值

Ale*_*ues 1 html jquery dom

我有一个非常简单的选择定义如下:

<select name='typedoc' id ='typedoc' size=1>
    <option>1</option>
    <option>2</option>
</select>
Run Code Online (Sandbox Code Playgroud)

有2个值,12.显示没问题.我无法使用jQuery更改该值.我试过了 :

$('[name=typedoc]').val('2');
Run Code Online (Sandbox Code Playgroud)

$('#typedoc').val('2');
Run Code Online (Sandbox Code Playgroud)

但是ddl仍然是第一选择.之前的行被执行,所以我确定该函数被调用.可能有什么不对?

Sel*_*gam 6

既然你的问题不是很清楚,我会假设你想要一个以下3个解决方案,

//To change the value of the option
$('[name=typedoc] option[value=2]').val('3'); 

//To select option with value = 3
$('[name=typedoc] option[value=3]').prop('selected',true);

//To Change the text of the option with value = 3
$('[name=typedoc] option[value=3]').text('Third');
Run Code Online (Sandbox Code Playgroud)