1

使用 PhoneGap,我有一个呈现为 UIPicker 的元素,其中包含 2 个选项。到目前为止,我未能以编程方式将第二个选项设置为选定的选项。

这是代码的样子(Javascript):

<select id="dropdown1" onchange"doSomething()">
  <option value="0">none</option>
  <option value="1">test</option>
</select>

  • 我未能使用“selectedIndex”选择第二个选项,即

    $("select#dropdown1").selectedIndex = 1;

  • 我也未能选择访问“选定”属性的第二个选项,即

    $("select#dropdown1 option")[1].attr("selected", "selected");

现在看上面2行代码生成的html,innerHTML保持不变,selected属性没有出现。我不知道刷新 GUI 的方法。任何人都可以阐明我做错了什么或者我是否遗漏了什么?

谢谢你。

约汉

4

1 回答 1

1

您需要确保刷新 jQuery Mobile 中的控件,以便更新漂亮的图形显示以对应于底层 HTML 标记。试试这个(http://jsfiddle.net/NPC42/m4jMn/4/):

// remove current selection
$("select#dropdown1 option").removeAttr("selected");

// add selected, and then refresh the parent control to see result
$("select#dropdown1 option[value='1']").attr('selected', 'selected').parent().selectmenu("refresh");

这对我有用。

于 2011-10-06T15:34:37.917 回答