如果满足特定条件,我正在尝试从下拉列表中选择第二个选项,我的selectedIndex有问题
<select id="contact" on-change="selectContact">
<option value="-1" selected>Select Contact</option>
<template is="dom-repeat" items="[[contacts]]" as="contact" index-as="index">
<option value="[[contact]]">[[contact]]</option>
</template>
</select>
<select id="name" on-change="selectName">
<option value="-1" selected>Select Name</option>
<template is="dom-repeat" items="[[names]]" as="name">
<option value="[[name]]">[[name]]</option>
</template>
</select>..。
selectContact() {
for (let i = 0; i < this.customerTable[0].length; i++) {
if(true) {
array[i] = this.customerTable[0][i]['name'];
}
}
this.names = this.$.util.utilFn(array);
if(this.names.length == 1) {
this.shadowRoot.querySelector('#name').selectedIndex = 2;
}
}如何选择dom-repeat下拉菜单的第二个子项?
发布于 2018-02-20 12:13:26
在selectContact()中,您设置了this.names (绑定了第二个<dom-repeat>.items ),并在DOM实际更新之前立即尝试选择该<dom-repeat>的第一项。
实际上,您需要等到使用Polymer.RenderStatus.afterNextRender()的下一个渲染帧后才能选择该项目:
selectContact() {
this.names = ['John'];
if (this.names.length === 1) {
Polymer.RenderStatus.afterNextRender(this, () => {
this.shadowRoot.querySelector('#name').selectedIndex = 1;
});
}
}https://stackoverflow.com/questions/48876125
复制相似问题