jquery - 使用 jQuery 选择选择元素中的第一个选项

标签 jquery jquery-mobile

我读过一些关于如何使用 jQuery 在 HTML Select 元素中选择第一个选项的文章,但我似乎无法让它在 jQuery 移动页面上工作。

选择元素:

<select id="myList">
    <option value="" selected>Select a List</option>
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
</select>

我调用脚本来选择第一个选项:

$("#myList").val($("#myList option:first").val());

我并不是试图获取选择元素中第一个选项的值,我实际上是试图选择它。

为了让它在 jQuery 移动页面上运行,我还需要做些什么吗?

谢谢。

最佳答案

一种解决方案是使用selectmenu("refresh")

refresh update the custom select: This is used to update the custom select to reflect the native select element's value. If the number of options in the select are different than the number of items in the custom menu, it'll rebuild the custom menu. Also, if you pass a true argument you can force the rebuild to happen.

//refresh value         
$('select').selectmenu('refresh');

//refresh and force rebuild
$('select').selectmenu('refresh', true);

source

html

<select id="myList">
    <option value="" selected>Select a List</option>
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
</select>

js

$("#myList").val($("#myList option:eq(1)").val());
$("#myList").selectmenu("refresh");

fiddle

关于jquery - 使用 jQuery 选择选择元素中的第一个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26961272/

相关文章:

javascript - 在 Jquery Mobile 中如何显示/隐藏 HTML 页面

javascript - 销毁多选小部件

jquery从特定id中删除特定元素

Javascript将日期字符串转换为DateTime类型(C#)格式问题

javascript - Jquery/Jquery 移动版 - Aptana Studio 3

windows-phone-7 - jQuery-Mobile 未在 Windows Phone 7 中运行?

javascript - 无法在phonegap中使用googlemaps进行地理定位

jquery-mobile - jQuery Mobile 中的可折叠内容

javascript - 使用 JQuery 获取单元格值

javascript - Typeahead.js 不显示预取数据的自定义模板