javascript focus()无法从第1行的tab1到tab2工作

时间:2017-02-14 09:52:06

标签: javascript validation primefaces

我正在使用功能,如果验证错误发生,那么goto链接会在页面顶部生成一个带有focus()的必需组件的id。单击该链接后,将突出显示所需的组件。现在它的工作如果错误在同一个标​​签中。但是第一个选项卡是主动对焦功能,而不是将所需的文本框指向第三个选项卡。

void encodeGotoLabel(FacesContext context, UIComponent component, ResponseWriter writer,
            FacesMessage msg) throws IOException {
            String id = getid(context, msg);
            if (id != null && !FacesMessage.SEVERITY_INFO.equals(msg.getSeverity())
                && !FacesMessage.SEVERITY_FATAL.equals(msg.getSeverity())) {
                String inputLabel = findInputLabel(context, id);

                if (inputLabel != null) {
                    ResourceBundle bundle ="bundle";

                    writer.writeText(" (", null);
                    writer.writeText(bundle.getString("goTo") + " ", null);
                    writer.startElement("a", component);
                    writer.writeAttribute("href", "javascript:document.getElementById('" + id
                        + "').focus();",
                        null);
                    writer.writeText(inputLabel, null);
                    writer.endElement("a");
                    writer.writeText(")", null);
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

此函数在一组选项卡上循环并检查是否有错误,如果是,则切换到它并专注于必填字段,您只需将其添加到您的函数中:

var tabs = PF('tabViewWv').getLength(); //tabViewWv is WidgetVar name
for (var iter = 1; iter <= tabs ; iter++) {
    if ($('#form\\:tabView\\:tab' + iter).find(':input').hasClass('ui-state-error')) {
        PF('tabViewWv').select(iter - 1);
        return false;
    }
}