2015-10-04 61 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