2015-08-27 35 views
0

請幫我在這裏解決以下問題。如何獲得由循環生成的一組dropDow的唯一ID?

代碼:

<%for(int ind=0;ind<15;ind++){%> 

<tr> 
    <struts-el:select name="OpEnh01pagSincomModelMaintanenceFormBean" property="mdlCode" styleId="mdlDrpDown_+'<%=ind %>'" onchange="modelCodeChanged(this.id)"> 
    <struts-el:options collection="listmodelCodes" property="modelCode" labelProperty="modelCodeDesc" /> 
    </struts-el:select> 
</tr> 

<%}%> 

1)我想在15行15個下拉菜單。我想dropDown的索引來確定哪個dropDown在JavaScript中被修改。

2)我想獲得一個唯一的ID,以便我可以在onChangeEvent調用的JavaScript中使用dom訪問它。

我有一個form bean的名稱屬性mdlDrpDown1,mdlDrpDown2等..

我想這種格式:styleId="mdlDrpDown_+'<%=ind %>'"但不能把它作爲styleId,產權屬性不runTimeExpr。

+0

平變化傳遞'this.id'但沒有設置id屬性。 –

+0

其實在生成的html。 「styleId」屬性被轉換爲「id」屬性。因此,我使用this.id作爲JS的參數。 但我的主要問題是我無法獲得屬性或StyleID屬性中的loopIndex。 – Vishnu300

回答

0

你是使用動態屬性名稱mdlCode<%=ind %> for select,如果getter和setter不是f在formbean中搜索No getter method Exception will be raised,爲了避免這種情況,我們可以index based properties(它可以保存多個基於索引的值,索引值將按照元素的順序出現在頁面中)而不是single properties(它只能保存一個值)。

試試下面的代碼:

的FormBean:

//create getter and setter for `mdlCode` using String[], so it can hold dynamic values. 

String[] mdlCode; 

public String[] getMdlCode() { 
    return mdlCode; 
} 
public void setMdlCode(String[] mdlCode) { 
    this.mdlCode=mdlCode; 
} 

JSP:

<%for(int ind=0;ind<15;ind++) { 
    String id = "mdlDrpDown_"+ind; 
%> 

<tr> 
    <!-- change property from `property="mdlCode<%=ind %>"` to property='mdlCode' --> 
    <struts-el:select name="OpEnh01pagSincomModelMaintanenceFormBean" property="mdlCode" styleId="<%=id %>" onchange="modelCodeChanged(this.id)"> 
     <struts-el:options collection="listmodelCodes" property="modelCode" labelProperty="modelCodeDesc" /> 
    </struts-el:select> 
</tr> 

<%}%> 
+0

我試過並得到錯誤: JSPG0124E:自定義標籤屬性styleId不能是運行時表達式。值:「[%= ID%]」 – Vishnu300

+0

現在我改變了styleId =「常量」 一個** **恆定>>頁加載和現在生成的HTML爲所有選擇元素具有: **名稱=「mdlCode」 id =「const」** 1)修改屬性屬性沒有幫助。 2)styleID **不能有任何rutime表達式**。所以我不能得到scriptlet <%=ind %>在這裏評估。 – Vishnu300

+0

@SalmanParacha:你可以檢查一次。 這個問題與您之前的帖子相似: [link](http://stackoverflow.com/questions/5970295/how-to-append-loop-index-of-cforeach-tag-to-struts-html -tag-attributes) – Vishnu300

-1

您styleId的格式會產生ID,如:

id="mdlDrpDown_+'1'" 
id="mdlDrpDown_+'2'" 

你應該寫styleId這樣的:

styleId="mdlDrpDown_<%=ind %>" 

那麼IDS會像:

id="mdlDrpDown_1" 
id="mdlDrpDown_2" 
+0

@ Brijesh: 試圖當我到了錯誤:styleId = 「mdlDrpDown_ <%=ind %>」 javax.servlet.jsp.JspException: 沒有可用的屬性的getter方法mdlDrpDown _%的豆= IND%。 <%=ind>未被評估。 – Vishnu300

+0

然後我試過了:styleId =「<%=ind %>」。 錯誤是: JSPG0124E:自定義標籤屬性styleId不能是運行時表達式。值:「[%=印度%]」 在tld文件中也是一樣的。 – Vishnu300

+0

@ Vishnu300不知道這個例外。但我認爲scriptlet在這裏幫不了你。 –