我想将一个Polymer纸张下拉菜单重置为JavaScript中的初始状态,因此没有选择任何内容,所以它看起来像这样:
我可以在纸张下拉菜单中的纸张菜单标签中添加一个ID,然后在JavaScript中访问它并选择它所选择的索引:
document.getElementById("accountTypeMenu").selected = 1;
但是,我只能选择一个可用项目,因此该数字必须为0或更大。我无法将其设置为-1以选择任何内容以在视觉上将其返回到其初始状态,但我可以将所选状态记录到我刚设置的状态。我试图更改为选中的其他值为null和undefined。
这是我用于纸张下拉菜单的html:
<paper-dropdown-menu
id="accountTypeDropdown"
selected-item="{{selectedItem}}"
selected-item-label="{{selected}}"
label='*Account type'
style="width:50%;"
noink
no-animations>
<paper-menu id="accountTypeMenu"
class="dropdown-content"
onmouseup="requiredMenuFieldSelected('accountType')">
<template is="dom-repeat"
items="{{accountTypes}}"
as="accountType">
<paper-item value="[[accountType.id]]">[[accountType.name]]</paper-item>
</template>
</paper-menu>
</paper-dropdown-menu>
<input is="iron-input"
name="accountType"
type="hidden"
value$="[[selectedItem.value]]">
答案 0 :(得分:9)
它是一个现成的领域。因此,您可能必须使用Polymer提供的函数来设置仅准备好的值。试试下面的
document.getElementById("accountTypeDropdown")._setSelectedItem({});
如果上述方法不起作用,请尝试下面的其他变体
document.getElementById("accountTypeDropdown")._setSelectedItem(null);
document.getElementById("accountTypeDropdown")._setSelectedItem();
答案 1 :(得分:3)
现在使用<paper-listbox></paper-listbox>
似乎更好。
有了它,您只需将selected
设置为null
:
<paper-dropdown-menu label="Type">
<paper-listbox class="dropdown-content" selected="{{myObj.type}}" attr-for-selected="value" id="list">
<paper-item value="0">Type 0</paper-item>
<paper-item value="1">Type 1</paper-item>
</paper-listbox>
</paper-dropdown-menu>
然后在JavaScript
:
this.$.list.selected = null;
答案 2 :(得分:1)
您可以手动触发事件:
document.querySelector("#accountTypeDropdown")._onIronDeselect();