Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道可以像这样获取当前选定项目的值:
var myListBoxItemText = $('#myListBox').val().toString();
但是如何将列表框中的这个值更改为其他值呢?
在评论中看到您的更新,这是一个更新的答案。只需使用:selected选择器过滤select元素内部的选项即可获得选定的选项。
:selected
select
要更改select元素的选定选项的value属性:
value
$('#myListBox option:selected').val('new value');
要更改其显示文本:
$('#myListBox option:selected').text('new text');
JSFiddle
$("#myListBox").val("This is the new value");
虽然,通过“列表框”,我假设你的意思是一个select元素。在这种情况下,您必须将该属性添加selected到其中一个子option元素。
selected
option
$('#myListBox').val(your_val);
这里 your_val 表示的value属性<option>
<option>
或者你也可以使用
$('#myListBox')[0].selectedIndex = 1;
或者
$('#myListBox').prop('selectedIndex', 1);
演示
演示 2