我正在尝试使用数组的值初始化一个选择菜单,我想要选择一个特定的条目。我可以使用此代码创建包含我想要的值的列表:
var myArray = new Array()
myArray = ["a", "b", "b", "d", "e", "f"];
function updateSelectMenu(){
for (var i = 0, len = myArray.length; i < len; i++) {
if (i == 2) {
$("#myArraySelectMenu")
.append($('<option>')
.attr('value', myArray[i])
.attr('selected', "selected")
.text(myArray[i]+"text"))
}
else {
$("#myArraySelectMenu")
.append($('<option>')
.attr('value', myArray[i])
.text(myArray[i]+"text"))
}
}
}
我的问题是:在点击查看所有选项之前,所选条目不会出现在下拉区域内。
Even if it is selected I cannot see the value at the top:
感谢您的帮助。
答案 0 :(得分:0)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Mobile Docs - Select</title>
<link rel="stylesheet" href="../../../jquery.mobile-1.0.1.min.css" />
<link rel="stylesheet" href="../../_assets/css/jqm-docs.css" />
<script src="../../../jquery.js"></script>
<script src="../../../experiments/themeswitcher/jquery.mobile.themeswitcher.js"></script>
<script src="../../_assets/js/jqm-docs.js"></script>
<script src="../../../jquery.mobile-1.0.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.each(['a', 'b', 'c', 'd', 'e', 'f'], function (i, value) {
if (i == 2) {
$("#myArraySelectMenu").append($('<option />', { 'value': value, 'selected': 'selected' }).text(value + 'text'));
}
else {
$("#myArraySelectMenu").append($('<option />', { 'value': value }).text(value + 'text'));
}
});
$("#myArraySelectMenu").selectmenu('refresh');
});
</script>
</head>
<body>
<div data-role="fieldcontain">
<label for="select-choice-7" class="select">
Array Mio:</label>
<select name="select-choice-7" id="myArraySelectMenu">
</select>
</div>
</body>
</html>