如何更改列表框中当前选定项目的值? [英] How to change the value of the currently selected item in the list box?

查看:154
本文介绍了如何更改列表框中当前选定项目的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以像这样获取当前选择的项目的值:

$ var myListBoxItemText = $('#myListBox')。 val()。toString();



但是,如何将列表框中的值更改为其他值?

解决方案

在评论中看到您的更新,这里有一个更新的答案。只需使用 :selected 选择器即可过滤内的选项,选择元素以获得选定的选项。



要更改选择元素的选定选项的属性:

  $('#myListBox选项:选中')。val('new value'); 

改变显示文字:

<$ p $(p> $('#myListBox option:selected')。text('new text');

JSFiddle


I know one can get the value of the currently selected item like this:

var myListBoxItemText = $('#myListBox').val().toString();

But how do you change this value in the list box to something else?

解决方案

Seeing your update in the comments, here's an updated answer. Just use the :selected selector to filter the options inside of your select element to get the selected option.

To change a select element's selected option's value property:

 $('#myListBox option:selected').val('new value');

To change its display text:

 $('#myListBox option:selected').text('new text');

JSFiddle

这篇关于如何更改列表框中当前选定项目的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆