【发布时间】:2014-12-27 20:39:49
【问题描述】:
我的代码在根据条件设置 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;
}
}
SelectElement 中的每个 <option> 都有一个名为 attribute_to_check 的唯一字符串属性,代码将要选择的所需选项与该属性进行比较。
问题是,如果 String 位于索引 0,我们称之为option0。
- 通过
option0,被选中的是option3, - 如果传递的字符串是
option1,则选择的选项是option5等等。
发生这种跳过模式的代码可能有什么问题?
【问题讨论】:
-
在我看来索引没有对齐。
se指的是this和child都以相同的索引被选中。对吗? -
是的,this.getElement() 是实际的 SelectElement