2015-10-04 39 views
0

我使用jstl foreach循環來顯示我的數據。用戶可以更改該值,最後我想通過jquery將這些數據發送到服務器。 我的問題是,當用戶更改數據並單擊「確定」按鈕時,如何將循環變量傳遞給我的jquery函數。 我用 <c:set var="chequeInfo" value="${cheque.chequeNo}~${cheque.amount}~${cheque.chequeDate}"/> 通過chequeInfo到​​函數。但這個變量的值,當用戶編輯我的數據將jstl foreach變量傳遞給javascript函數

<c:forEach items="${chequeList}" var="cheque"> 
<div class="row"> 
    <input class="date" type="text" 
      value="${cheque.chequeDate}"> 

    <select id="chequeStatus"> 
     <c:forEach items="${chequeStatus}" var="status"> 
      <option value="${status.id}">${status.farsiName}</option> 
     </c:forEach> 
    </select> 

    <label class="col-lg-3">${cheque.chequeNo} </label> 

    <input type="text" value="${cheque.amount}"> 

    <button class="col-lg-offset-1 col-lg-1 btn btn-default register-edit" 
      onclick="registerEdit('${cheque}','${status.id}')">ok 
    </button> 
</div> 
</c:forEach> 

我怎麼能發送${cheque}${status.id}到​​功能並沒有改變。謝謝

回答

0

你可以爲你的foreach標籤添加varStatus屬性,爲你的輸入創建唯一的id,並將index的值傳遞給你的registerEdit。

<c:forEach items="${chequeList}" var="cheque" varStatus="loop"> 
<div class="row"> 
    <input id="chequeDate-${loop.index}" class="date" type="text" 
      value="${cheque.chequeDate}"> 

    <select id="chequeStatus-${loop.index}"> 
     <c:forEach items="${chequeStatus}" var="status"> 
      <option value="${status.id}">${status.farsiName}</option> 
     </c:forEach> 
    </select> 

    <label class="col-lg-3">${cheque.chequeNo} </label> 

    <input id="amount-${loop.index}" type="text" value="${cheque.amount}"> 

    <button class="col-lg-offset-1 col-lg-1 btn btn-default register-edit" 
      onclick="registerEdit('${loop.index}')">ok 
    </button> 
</div> 
</c:forEach> 

在你的js函數

function registerEdit(index){ 
//get chequeStatus value like this 
var chequeStatus = $("#chequeStatus-"+index).val() 

//do same thing for other values 

} 
+0

謝謝你真的幫我 – farhad

+0

ID = 「chequeDate - $ {} loop.index」 – farhad