如何在服务器中增加值的jsp中添加新行

时间:2014-06-27 06:33:18

标签: jquery jsp struts2

这是我在div中添加另一行的代码。单击#add,它将获取新行并追加到.panel-body此代码正常工作但我无法在新添加的行中设置递增值。

 $(document).ready(function() {
        $(document).on('click', '#add', function(event) {
        event.preventDefault();
                $.ajax({
                type: "POST",
                        url: "getNewRow",
                        data: 1,
                        dataType: "html",
                        success: function(data) {
                        $(".panel-body").append(data);
                        }
                });
        });

在struts.xml中

<action name="getNewRow" class="iland.expense.ExpenxeAction" method="getNewRow">
            <result name="success">/pages/expense/NewRow.jsp</result>
            <result name="input">/pages/expense/NewRow.jsp</result>
            <result name="login">/pages/login.jsp</result>            
            <result name="UNAUTHORIZED">/pages/Unautho.jsp</result>
        </action>

在NewRow.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<div class="row" id="<s:property value="id"/>">
    <div class="col-xs-1"></div>
    <div class="form-group col-xs-1 col-sm-6 col-lg-3">
        <label>Expensed Type</label>
        <s:select name="expenseList[0].param" 
                  list="'advertisement','food'"/>
    </div>
    <div class="form-group col-xs-1 col-sm-6 col-lg-3">
        <label>Amount</label>
        <s:textfield name="expenseList[0].value" value="%{expenseList[0].value}"/>
    </div>
    <div class="form-group col-xs-1 col-sm-6 col-lg-3">
        <label>Date</label>
        <s:textfield name="expenseList[0].dt" value="%{expenseList[0].dt}"/>
    </div>
    <div class="form-group col-xs-1 col-sm-1 col-lg-1 cl">
        <label ></label>
        <img src="images/delete.png"/>
    </div>

</div>

此处<s:property value="id"/>是递增值,我想在NewRow.jsp中加入,并希望将expenseList[0]设置为expenseList[1]

如何做到这一点。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。 在行动中,我将值递增1并在NewRow.jsp中进行更改,如下所示

<%@taglib uri="/struts-tags" prefix="s"%>
<div class="row" id="<s:property value="rowId"/>">
    <div class="col-xs-1"></div>
    <div class="form-group col-xs-1 col-sm-6 col-lg-3">
        <label>Expensed Type</label>
        <s:select name="expenseList[%{rowId}].param" 
                  list="'advertisement','food'"/>
    </div>
    <div class="form-group col-xs-1 col-sm-6 col-lg-3">
        <label>Amount</label>
        <s:textfield name="expenseList[%{rowId}].value" value="%{expenseList[%{rowId}].value}"/>
    </div>
    <div class="form-group col-xs-1 col-sm-6 col-lg-3">
        <label>Date</label>
        <s:textfield name="expenseList[%{rowId}].dt" value="%{expenseList[%{rowId}].dt}"/>
    </div>
    <div class="form-group col-xs-1 col-sm-1 col-lg-1 cl">
        <label ></label>
        <img src="images/delete.png"/>
    </div>

%{rowId}解决了我的问题。