2014-03-25 52 views
-1

我需要將每個項目的ID添加到onchange事件的功能中,但<select>標記不接受表達式。如何將for循環中的每個項目的id傳遞給javascript函數?

一旦用戶從列表中選擇了一個類別,相關項目的id以及值應該被髮送到Ajax以發送到後端。

<c:forEach var="item" items="${Items}"> 
     ..... 
     <s:select id="name" 
       name="name" 
       list="@[email protected]" 
       value="item.category" 
       onchange='changeItemCategory(${item.id},this.value)' 
     /> 
</c:forEach> 

它不接受%{} item.id爲好,當我嘗試item.id它發送請求的功能,但proid變量的值將被「未定義」。

<script> 

function changeItemCategory(proid,cat){ 
    alert(proid+cat); 
    ..... 
} 

</script> 

錯誤:

According to TLD or attribute directive in tag file, attribute `onchange` does not accept 
any expressions. 
+0

什麼是'id'?它是一個int值還是一個字符串 –

+0

id是一個整數。 – AlexCartio1

+0

是不是通過選擇標籤接受的EL表達式?檢查你是否isELIgnored = true –

回答

0

使用OGNL,而不是JSP EL:

onchange='changeItemCategory(%{#item.id}, this.value)' 
+0

它不調用函數,FireBug顯示,changeItemCategory(,this.value)並且在逗號下有一個箭頭。 FireBug的錯誤是SyntaxError:語法錯誤 – AlexCartio1

+0

也在頁面源代碼中,它就像changeItemCategory(,this.value) – AlexCartio1

+0

@ AlexCartio1哦,你用jstl迭代,用s2迭代器代替,對不起。 –

相關問題