【问题标题】:Set selected option of a SelectElement设置 SelectElement 的选定选项
【发布时间】: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 中的每个 &lt;option&gt; 都有一个名为 attribute_to_check 的唯一字符串属性,代码将要选择的所需选项与该属性进行比较。

问题是,如果 String 位于索引 0,我们称之为option0

  • 通过option0,被选中的是option3
  • 如果传递的字符串是option1,则选择的选项是 option5 等等。

发生这种跳过模式的代码可能有什么问题?

【问题讨论】:

  • 在我看来索引没有对齐。 se 指的是thischild 都以相同的索引被选中。对吗?
  • 是的,this.getElement() 是实际的 SelectElement

标签: java algorithm loops gwt


【解决方案1】:

我敢打赌选择中有非元素子元素。

仅在 Element.is(el) 或更好地循环 SelectElement#getOptions() 时尝试增加 i

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2020-03-04
    • 2023-03-04
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多