使用jQuery设置<option>元素的文本

Tob*_*oby 10 html javascript jquery select

使用jQuery,为给定的选择值编辑选项文本的最佳方法是什么.

我知道我想编辑的选项的价值.我以为它会像......

$('#select').val().$('option').html('New Text');
Run Code Online (Sandbox Code Playgroud)

但我显然遗漏了一些东西.

Anu*_*rag 20

$('#select option[value="someValue"]').text("newText")
Run Code Online (Sandbox Code Playgroud)

如果添加HTML内容,可以使用.html而不是.text.


kie*_*ran 8

像这样的东西?

$('#select').children('option').text('New Text');
Run Code Online (Sandbox Code Playgroud)

有关选择正确项目的更多信息,请查看选择器.你可以做点什么

$('#select').children('option:first').text('New Text');
Run Code Online (Sandbox Code Playgroud)

或者你可以使用

$('#select').children('option[value="thevalue"]').text('New Text');
Run Code Online (Sandbox Code Playgroud)

如果你知道价值.


Fré*_*idi 5

您可能正在寻找:selected选择器和text()方法:

$("#select option:selected").text("New Text");
Run Code Online (Sandbox Code Playgroud)