测试是否选中了收音机和复选框按钮

时间:2016-06-13 09:54:33

标签: javascript java struts-1

我想知道我的单选按钮和复选框是否已选中。如果是这样,我将保存数据库中的更改。 这是我的表单代码:

 <SCRIPT>
    function set(target) {
    var val1;
    if (target == 'save') {

        val1 = getradiovalue('officialForCalculation');
        document.forms[1].selectedOfficialCalc.value = val1;

    }
    document.forms[1].button.value= target;
    document.forms[1].submit();
}

function getradiovalue(rad) {
    var rad_val;
        if (rad == 'officialForCalculation')
            if (document.forms[1].officialCalculation) {
                if (!document.forms[1].officialCalculation[1]) {
                    if (document.forms[1].officialCalculation.checked) {
                        rad_val = document.forms[1].officialCalculation.value;
                    }
                } else {
                    for (var i = 1; i < document.forms[1].officialCalculation.length; i++) {
                        if (document.forms[1].officialCalculation[i].checked) {
                            rad_val = document.forms[1].officialCalculation[i].value;
                        }
                    }
                }
            }

    return rad_val;

}

</SCRIPT>
        <form action="/getAll.do" name="table">
            <logic:present name="frmTransitionMatrix" property="matrixes">
            <logic:iterate name="frmTransitionMatrix" property="matrixes" id="matrixid">
            <c:if test="${matrixid.officialForCalculation=='1'}">
                <td align="center" width="25%"><input type="radio"
                                                          name="officialForCalculation"
                                                          value="<bean:write 

name="matrixid" property="idTransitionMatrix"/>"                                                checked='checked'/>
                </td>
            </c:if>

            <c:if test="${matrixid.officialForCalculation=='0'}">
                <td align="center" width="25%"><input type="radio"
                                                      name="officialForCalculation"
                                                      value="<bean:write name="matrixid" property="idTransitionMatrix"/>"/>
                </td>
            </c:if>

            <logic:equal value="1" name="matrixid" property="active">
                <td align="center" width="25%"><input type="checkbox"
                                                      name="activeMatrix"
                                                      value="<bean:write name="matrixid" property="idTransitionMatrix"/>"
                                                      checked /></td>
            </logic:equal>
            <logic:equal value="0" name="matrixid" property="active">
                <td align="center" width="25%"><input type="checkbox"
                                                      name="activeMatrix"
                                                      value="<bean:write name="matrixid" property="idTransitionMatrix"/>"/>
                </td>
            </logic:equal>
</tr>
            </logic:iterate>
            </logic:present>
      <input type="button" name="button" value="Save" class="button" onClick="set('save');"/>
    </form>

这是我的MatrixForm

@Data
public class MatrixForm extends ActionForm {

private List<MatrixEntity> matrixes = new ArrayList<MatrixEntity>();
private String allMatrixes;
private String selectedOfficialCalc;
private String[] activeMatrix;

}

我的动作类

public ActionForward updateAll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws DBAccessException, TimeOutException {

    MatrixForm faForm = (MatrixForm) form;
    MatrixDaoImpl matrixDao = new MatrixDaoImpl();
    String MatrixForCalc = faForm.getSelectedOfficialCalc();
    List<String> validForCalcList = new ArrayList<String>();
    validForCalcList.add(MatrixForCalc);
    String[] activeMatrix = faForm.getActiveMatrix();
    List<String> actMatrixsList = new ArrayList<String>();
    if (activeMatrix != null) {
        for (int i = 0; i < activeMatrix.length; i++) {
            actMatrixsList.add(activeMatrix[i]);
        }
    }

    List<MatrixEntity> matrixes = faForm.getMatrixes();
    MatrixEntity Entity;

    if (matrixes != null && !matrixes.isEmpty()) {

        for (MatrixEntity matrix : matrixes) {
            if (validForCalcList != null && !validForCalcList.isEmpty()
                    && validForCalcList.contains(matrix.getIdTransitionMatrix())) {
                if (!actMatrixsList.contains(matrix.getIdTransitionMatrix())) {
                    //getB2SContext(request).setErrorInfo("The Official Portfolio for Calculation cannot be inactive");
                    return mapping.findForward("active");
                }
            }
        }

        for (int count = 0; count < matrixes.size(); count++) {
            Entity =  matrixes.get(count);
                if (validForCalcList != null && !validForCalcList.isEmpty()
                        && validForCalcList.contains(Entity.getIdTransitionMatrix())) {
                    if (!actMatrixsList.contains(Entity.getIdTransitionMatrix())) {
                        //getB2SContext(request).setErrorInfo("The Official Portfolio for Calculation cannot be inactive");
                        return mapping.findForward("active");
                    } else {
                        Entity.setActive(BigInteger.ONE);
                        Entity.setOfficialForCalculation(BigInteger.ONE);
                    }
                } else {
                    if (actMatrixsList.contains(Entity.getIdTransitionMatrix())) {
                        Entity.setActive(BigInteger.ONE);
                    } else {
                        Entity.setActive(BigInteger.ZERO);
                    }
                    Entity.setOfficialForCalculation(BigInteger.ZERO);
                }

        }

        matrixDao.updateAll(matrixes);

    }

我调试了我的代码,我发现activeMatrix即使被检查也是null,并且selectedOfficialCalc也是如此。我想知道如果选中MatrixForm的属性我将如何设置它。

0 个答案:

没有答案