0

我的目标是将焦点设置为特定的 inputText。

由于似乎无法使用 tr:inputText 属性自动执行此操作,因此我尝试使用 Javascript。

因为document.getElementById我需要渲染的 html 的 id <input>。我尝试将此 id 设置为正确的索引。但是,它使用 id 呈现,varFieldIterator:0:varField其中数字 0 来自迭代器,并且根据我的起始值 selectionStart 不同。如果我从 selectionStart = 10 开始,文本框 10 的第一个索引是 0。如果我从 15 开始,然后有一个 PPR 来显示前 15 个文本框,它们会得到从 16 开始的 id。

我认为代码将明确我试图实现的目标:

<tr:iterator
    id="varFieldIterator"
    value="#{myController.form.model.fieldList}"
    var="field"
    rows="15"
    first="#{{myController.form.model.selectionStart}"
    varStatus="status">
  <tr:inputText
      id="varField#{status.index}"
      value="#{field.value}"
      label="Text #{status.index +1}">
</tr:iterator>

<tr:inputHidden
    id="FOCUS_TEXT"
    value="#{myController.form.model.endFocus}"></tr:inputHidden>
<trh:script>
window.onload = function() { document.getElementById('varField' + document.getElementById('FOCUS_TEXT').value).focus(); } 
</trh:script>
4

1 回答 1

1

虽然 ID 属性接受 EL 表达式,但它仅在视图构建时设置,而不是在视图渲染时设置。因此,如果您使用诸如 JSTL 之类的视图构建时间标记,<c:forEach>它会起作用。但是这个标签可能有不受欢迎的副作用

我建议只更改您的 JavaScript 函数,使其不再依赖于 ID。目前尚不清楚您的整个 HTML dom 结构是什么,以及您是否使用jQuery,这将简化 HTML DOM 遍历。

例如,使用普通 JS,您可以只抓取第一个表单,然后抓取它的第一个输入元素:

document.forms[0].elements[0].focus();

或者您可以通过标签名称获取输入元素

document.getElementsByTagName("input")[0].focus();

或者你可以给他们一个类名,比如通过类名<tr:inputText styleClass="specificInput">获取它们

document.getElementsByClassName("specificInput")[0].focus();

这些调用是可链接的。所以如果你知道它在 a<h:form id="myForm">那么你也可以这样做:

document.getElementById("myForm").getElementsByTagName("input")[0].focus();
于 2012-02-23T16:38:05.707 回答