Ajax提交后如何复制当前选定的选项值

时间:2018-08-12 15:46:01

标签: java jquery struts2 struts2-jquery

我有一个下拉菜单“ company”,位于form1下。提交form2(它是一个模态)时,我需要复制(或保留)它的当前值(和名称)。我成功完成了输入操作,使用了来自struts2的值(通过会话放置),但是没有选择,提交后未选择正确的值。

<form name="form1" type="submit">
<div class="row">
    <div class="col-md-3">
        <div class="form-group label-floating">
            <label class="control-label">P.O. Number</label>
            <input type="text" class="form-control" name="poNumber" value="poNumber">
        </div>
    </div>
    <div class="col-md-3">
        <select
            class="form-control"
            id="companies"
            name="companyName" title="Companies">
                <s:if test="company neq null">
                    <s:iterator value="company">
                            <option value="<s:property value="id"/>"><s:property value="name"/></option>
                    </s:iterator>
                </s:if>
        </select>
    </div>
</div>

这是我要提交的模式表单

<form name="form2" action="<s:url action="someaction" namespace="/samplenamespace"/>">
       ...
        <button type="button" id="btn-cancel" data-dismiss="modal" class="btn btn-secondary">cancel</button>
        <button type="submit" id="add-order-item" class="btn btn-raised btn-primary">add</button></form>

这是jquery:

$('#add-order-item').on('click', function(e){
        e.preventDefault();
        var po_number = $('<input />',
                {class:'form-control',
                    name: 'poNumber',
                    value: $('input[name="poNumber"]').val()
                });                 
    var formOrderDetails = $('form[name="orderDetailForm"]');           
            formOrderDetails.append(po_number);
        $('body').append(formOrderDetails);
        formOrderDetails.submit();});

这是struts2 java

public class EncodePurchaseDetail extends ControllerSupport<EncodePurchaseDetail>{

private String poNumber;
private String companyName;


private List<CompanyEntity> company = new ArrayList<CompanyEntity>();

@SuppressWarnings("unchecked")
@Action(value="EncodePurchaseDetail",  results = {...})
public String encodeDetail(){
    ..
    List<OrderDetailsEntity> orderDetails = null;
    PurchaseOrderEntity po = null;
    dao = HibernateDAOFactory.getInstance();

    if(session.containsKey("po")) {
        po = ((PurchaseOrderEntity) session.get("po"));
        po.setPoNumber(getPoNumber());
        po.setCompanyName(getCompanyName());
    } else {
        po = new PurchaseOrderEntity();
        po.setPoNumber(getPoNumber());
        po.setCompanyName(getCompanyName())
        ...
    }

    session.put("orderDetails", orderDetails);
    session.put("po", po);

    return SUCCESS;
}

0 个答案:

没有答案