javascript - 如何使用jquery更改选择框中呈现的选定选项

标签 javascript jquery html jquery-mobile drop-down-menu

我尝试了很多方法来使用 jquery 选择一个选项,包括 .val("0") .attr('selected', 'selected').attr('selected', 'true')TexoTela plugin .他们似乎都更新了 html,但浏览器仍然显示用户选择的最后一个项目。

试用 this fiddle ... 选择下拉值 1,然后单击链接。

有什么方法可以实际更新显示的值吗?

最佳答案

您的意思是更改选中的项目?

$('select').val('0');

还有其他方法,但这是基本的,以您为例。

jsfiddle

您可以阅读 .val() 的文档 here ,以及片段:

checks, or selects, all the radio buttons, checkboxes, and select options that match the set of values.

为 jQuery Mobile 编辑

对于 jQuery Mobile,您还需要按照 mobile docs 中所述刷新选择:

If you manipulate a select via JavaScript, you must call the refresh method on it to update the visual styling.

您可以通过以下方式做到这一点:

$('select').selectmenu("refresh",true);

关于javascript - 如何使用jquery更改选择框中呈现的选定选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6222559/

相关文章:

html - 子菜单链接填充

javascript - 如何在 IE 中将灰度和反转效果与 SVG 滤镜结合起来?

jquery - 使用 content_for 和 ajax/Jquery 部分更新网页

javascript - 使用 Javascript 检查 m3u8 URL 是否损坏

javascript - 使用 CSS 或 JS 识别设备类型

html - 在 html 文件中使用 sass

javascript - HTML5 for IE6.0

javascript - 你能让浏览器检测除 <a> 之外的元素上的 Javascript 链接吗

javascript - API回调

javascript - 使用javascript将文本从一个文本框复制到另一个文本框