Setting the text of an

2020-07-03 06:40发布

Using jQuery what is the best way of editing option text for a given select value.

I know the value of the option I want to edit. I thought it would be something like...

$('#select').val().$('option').html('New Text');

But I am clearly missing something.

4条回答
我欲成王,谁敢阻挡
2楼-- · 2020-07-03 07:01

Something like this?

$('#select').children('option').text('New Text');

Have a look at Selectors for more information on selecting the correct item. You could do something like

$('#select').children('option:first').text('New Text');

Or you can use

$('#select').children('option[value="thevalue"]').text('New Text');

If you know the value.

查看更多
叼着烟拽天下
3楼-- · 2020-07-03 07:07

This one help you to get the options value,

$('#select_id').children('option[value="thevalue"]').text('New Text');

And you can set default selected value by using prop() function. Take an example here.

查看更多
家丑人穷心不美
4楼-- · 2020-07-03 07:10
$('#select option[value="someValue"]').text("newText")

could use .html instead of .text, if adding html content.

查看更多
地球回转人心会变
5楼-- · 2020-07-03 07:25

You're probably looking for the :selected selector and the text() method:

$("#select option:selected").text("New Text");
查看更多
登录 后发表回答