【问题标题】:PhoneGap iOS UIPicker equivalent element: select an option value programmaticallyPhoneGap iOS UIPicker 等效元素:以编程方式选择选项值
【发布时间】:2011-10-05 20:32:32
【问题描述】:

使用 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 的方法。谁能阐明我做错了什么或者我遗漏了什么?

谢谢。

约翰

【问题讨论】:

    标签: javascript ios cordova option uipicker


    【解决方案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");
    

    这对我有用。

    【讨论】:

    • 它不适用于 JQuery 版本 1.4.x。它适用于 1.6+ 版本。那是我的问题。
    猜你喜欢
    • 2015-12-24
    • 2021-01-02
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2017-07-09
    相关资源
    最近更新 更多