2012-02-23 76 views
0

我的目標是將焦點設置爲特定的inputText。在迭代器中爲inputText設置ID

由於似乎沒有辦法使用tr:inputText屬性自動執行此操作,因此我嘗試使用Javascript。

對於document.getElementById我需要呈現的html <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> 

回答

1

雖然ID屬性接受EL表達式,它只有在被查看生成的時間設置,而不是在視圖渲染時間。因此,如果您使用的是視圖編譯時間標記(如JSTL <c:forEach>),它會起作用。但是這個標籤可能有undesireable side effects

我建議只更改您的JavaScript函數,使其不再依賴於ID。目前還不清楚你的整個HTML DOM結構是什麼,如果你使用的是jQuery或者不是,這將簡化HTML DOM遍歷。

用純JS,你可以,例如只需grab the first form,然後抓住它的第一輸入元件:

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

,或者你可以grab input elements by tag name

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

或者你可以給他們所有的類名如<tr:inputText styleClass="specificInput">grab them by class name

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

這些調用是可鏈接的。所以,如果你知道這是一個<h:form id="myForm">裏面,那麼你也可以做,例如:

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

雖然最簡單的辦法設置獨立的迭代器的數字沒有工作的ID,你的建議,但幫助找到一個解決辦法。 – stracktracer 2012-02-24 13:24:54