我的代码有问题,无法根据条件设置 SelectElement 的选定选项:
@Override
public void setModel(String s) {
int children = this.getElement().getChildCount();
int i = 0;
while(i < children){
Node child = this.getElement().getChild(i);
final Element el = child.cast();
if (Element.is(el)) {
if(el.getAttribute("attribute_to_check") != null){
if(el.getAttribute("attribute_to_check").equalsIgnoreCase(s)){
SelectElement se = this.getElement().cast();
se.setSelectedIndex(i);
}
}
}
++i;
}
}
每个<option>
在SelectElement
有一个名为 attribute_to_check
的唯一字符串属性代码将要选择的所需选项与之进行比较。
问题是,如果位于索引 0 处的字符串,我们称它为 option0
.
- 路过
option0
, 被选中的是option3
, - 如果传递的字符串是
option1
选择的选项是option5
等等。
发生这种跳过模式的这段代码可能有什么问题?
最佳答案
我敢打赌选择中有非元素子元素。
尝试仅在 Element.is(el)
时递增 i
,或者更好的是,循环遍历 SelectElement#getOptions()
。
关于java - 设置 SelectElement 的选定选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27671270/