如何将复选框与文本框关联,以便在数据库中更新所选复选框和文本框的相应值

时间:2016-09-20 10:12:43

标签: java jquery jsp spring-mvc jdbctemplate

我想将复选框ID(动态创建)与文本框相关联。

例如:如果5个复选框与5个文本框一起出现,我希望选中相应值的复选框将在数据库中获得更新。

问题:如果在jsp页面上出现5个复选框,并且如果我选择2,4和5复选框,则应在数据库中插入相应的文本框值,即2,4和5值。 但在我的情况下,正确插入复选框但不插入值。插入类似于第二个复选框--->它正在选择第一个文本框, 和第四个复选框---->它正在选择第二个文本框值,第五个复选框则选择-----> 3文本框值。

我知道这是因为这段代码:这里属性列表包含三个选中的复选框,但属性值包含5个元素。它只会选择三个。

List<Object[]> inputList = new ArrayList<Object[]>();
for (int i = 0; i < qualification.getAttributeList().length; i++) {
    Object[] tmp = {newCompanyId,qualification.getAttributeList()[i],qualification.getAttributeValue()[i]};
    inputList.add(tmp);

任何人都可以告诉我解决问题。请找到附带的照片。 Page for adding new attributes 资格控制器

@RequestMapping(value =&#34; / save&#34;,method = RequestMethod.POST) public ModelAndView save(ModelAndView模型,HttpServletRequest req,HttpServletResponse res,         @ModelAttribute资格认证,HttpSession会话)抛出AppException {

String userId = session.getAttribute("loggedInUserId").toString();
qualificationDAO.save(qualification, userId);

model.setViewName("qualification");
return model;

}

Qualification.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="tg" tagdir="/WEB-INF/tags"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<jsp:useBean id="pagedListHolder" scope="request" class="org.springframework.beans.support.PagedListHolder" />

<!-- Main Body Section Start -->
<main class="container padContnr">
  <div class="row">
    <div class="col-md-12">
      <h1>Qualification Rules</h1>
    </div>
  </div>


  <div class="bg-ltgreen padding-10 margin-t-10" id="QualEdit">
    <form:form class="form-horizontal" action="save" modelAttribute="qualification" method="post" >

      <div class="row">
        <div class="col-sm-6 col-xs-12">


          <div class="form-group">
            <label class="label-bg col-xs-12">Company <span class="red">*</span></label>
            <div class="col-xs-12">
             <form:select id="company" class="form-control" path="company">
                <form:option value="Select" />
                <form:options items="${companyList}" itemValue="id" itemLabel="dropdownValue"/>
            </form:select>
            </div>
          </div>


          <div class="form-group">
            <label class="label-bg col-xs-12">Class <span class="red">*</span></label>
            <div class="col-xs-12">
             <form:select id="companyClass" class="form-control" path="companyClass" onchange="switchOption();">
                <form:option value="Select" />
                <form:options items="${classList}" itemValue="id" itemLabel="dropdownValue"/>
            </form:select>              
            </div>
          </div>

      </div>

         <div class="col-sm-6 col-xs-12">

<%--           <div class="form-group">
            <label class="label-bg col-xs-12">Status <span class="red">*</span></label>
            <div class="col-xs-12">
              <form:select class="form-control" path="status" >
                <form:option value="Select"></form:option>
                <form:options items="${statusList}" itemValue="id" itemLabel="dropdownValue" />
              </form:select>
            </div>
          </div> --%>



           <div class="form-group">
            <label class="label-bg col-xs-12">Rule <span class="red">*</span></label>
            <div class="col-xs-12">
              <form:input path="rule" class="form-control" type="text" />
            </div>
          </div>

        </div>

      </div>
     <div id="QualAtt"class="row margin-t-10">
        <div id="QualAtt1" class="col-md-6">

        <!-- table data -->
        </div> 
      </div> 


      <div class="row">
        <div class="col-xs-12 margin-t-10">
         <!--  <button class="btn btn-primary btnSubmit" type="submit" id="QualSav">Save</button> -->
          <button type="submit" class="btn btn-primary btnSubmit">                                                                                                                                                                                  </button>
          <button class="btn btn-default btnCancel" type="button" id="QualCanl">Cancel</button>
        </div>
      </div>

    </form:form>
  </div>

</main>

资格模型

package com.hcl.ne.model;

import java.util.HashMap;
import java.util.Map;

/**
 * @author diwakar_b
 *
 */

public class Qualification {

    private int id;
    private String company;
    private String companyClass;
    private String rule;
    private String lastModifiedOn;
    private String lastModifiedBy;
    private String status;
    private String[] attributeList;
    private String[] attributeValue;
    private int pagesize;
    private String ruleId;
    private String[] attributeListNames;
    private String[] attributeValueNames;
    private String classId;
    private String attributeId;
    private String attributeName;
    private String attributeValues;


    public String[] getAttributeValue() {
        return attributeValue;
    }
    public void setAttributeValue(String[] attributeValue) {
        this.attributeValue = attributeValue;
    }
    public String getAttributeValues() {
        return attributeValues;
    }
    public void setAttributeValues(String attributeValues) {
        this.attributeValues = attributeValues;
    }
    public String getAttributeName() {
        return attributeName;
    }
    public void setAttributeName(String attributeName) {
        this.attributeName = attributeName;
    }

    public String getAttributeId() {
        return attributeId;
    }
    public void setAttributeId(String attributeId) {
        this.attributeId = attributeId;
    }
    public String[] getAttributeList() {
        return attributeList;
    }
    public void setAttributeList(String[] attributeList) {
        this.attributeList = attributeList;
    }
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    public String getCompanyClass() {
        return companyClass;
    }
    public void setCompanyClass(String companyClass) {
        this.companyClass = companyClass;
    }
    public String getRule() {
        return rule;
    }
    public void setRule(String rule) {
        this.rule = rule;
    }

    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getLastModifiedOn() {
        return lastModifiedOn;
    }
    public void setLastModifiedOn(String lastModifiedOn) {
        this.lastModifiedOn = lastModifiedOn;
    }
    public String getLastModifiedBy() {
        return lastModifiedBy;
    }
    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public int getPagesize() {
        return pagesize;
    }
    public void setPagesize(int pagesize) {
        this.pagesize = pagesize;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getRuleId() {
        return ruleId;
    }
    public void setRuleId(String ruleId) {
        this.ruleId = ruleId;
    }

    public String[] getAttributeListNames() {
        return attributeListNames;
    }
    public void setAttributeListNames(String[] attributeListNames) {
        this.attributeListNames = attributeListNames;
    }
    public String[] getAttributeValueNames() {
        return attributeValueNames;
    }
    public void setAttributeValueNames(String[] attributeValueNames) {
        this.attributeValueNames = attributeValueNames;
    }
    public String getClassId() {
        return classId;
    }
    public void setClassId(String classId) {
        this.classId = classId;
    }


}

PLugin.js

$('#QualSav, #QualCanl').on('click', function() {
    $("#QualEdit").hide();
    $("#QualRules").hide();
    $("#QualSerch").show();
}); 

// Qualification -- Edit

$('#EditBtnQualification').on('click', function() {
    var ruleId=$('table.trClick tbody tr.trActive td:first').html();
       $('#ruleId').val(ruleId);
       document.qualificationEditForm.submit();
});


function switchOption() {
    var  selectedValue= $("#companyClass").val();
    $.ajax({
        type: "GET",
        url: "getClass",
        data:'classId='+selectedValue,
        success: function(data){

            var tablebody = $('<tbody>');
            var tablerow = "";
            $(data.resultMenuForLocation).each(function(index){
                tablerow = $('<tr>')
                    .append($('<td>').append($('<input>').attr({type :'checkbox'}).attr('class','checkBoxSize').attr({value : ''+$(this)[0].id}).attr('id','attributeList').attr('name','attributeList')))
                    .append($('<td>').append($(this)[0].dropdownValue+''))
                    .append($('<td>').append($('<input>').attr({type :'text'}).attr('class','form-control').attr('id','attributeValue').attr('name','attributeValue')))
                $(tablebody).append(tablerow);
            });
            $('<table>')
            .attr('id','master_table')
            .attr('class','myAttr f-size-16')
            .html('<thead><tr><th>&nbsp;</th><th>Attribute</th><th>Value</th></tr></thead>')
            .append(tablebody)
            .appendTo('div#QualAtt1');

        }

});

};

0 个答案:

没有答案