struts验证后启用禁用字段

时间:2017-01-11 08:10:23

标签: javascript struts

我使用java脚本和struts来验证表单。

我有'日期'字段和复选框。选中复选框后,“日期”字段将被禁用

问题是,在struts验证后,我的禁用字段再次启用,并且仍然显示验证错误消息

`<tr class="table_row_odd">
    <td><label class="mandatory">*</label>Date (DD/MM/YYYY) : </td>
    <td>
        <html:text property="day" size="2" maxlength="2" styleClass="form_field" />&nbsp;/&nbsp;
        <html:text property="month" size="2" maxlength="2" styleClass="form_field" />&nbsp;/&nbsp;
        <html:text property="year" size="4" maxlength="4" styleClass="form_field" />
    </td>
    <td>&nbsp;</td>
</tr>

<tr class="table_row_odd">
    <td> </td>
    <td colspan="3">
        <html:checkbox property="dateUnknown" value="Y" onclick="javascript:check()" ></html:checkbox>
        Please check if date unknown
    </td>
</tr>
function check()
 {
 if(window.document.getElementById("dateUnknown").checked==true)
 {  window.document.getElementById("day").disabled= true;
    window.document.getElementById("month").disabled= true;
    window.document.getElementById("year").disabled= true;
} }
<form name="/create">
<field property="date"   
            depends="validateDateRequired">
            <arg0 key="errors.required.date" />
            <var>
                <var-name>dayfield</var-name>
                <var-value>day</var-value>
            </var>
            <var>
                <var-name>monthfield</var-name>
                <var-value>month</var-value>
            </var>
            <var>
                <var-name>yearfield</var-name>
                <var-value>year</var-value>
            </var>
        </field>
     </form>
public static boolean validateDaterequired(Object bean, ValidatorAction va, 
        Field field, ActionMessages errors, Validator validator, HttpServletRequest request)
{
    boolean returnValue = true;

    try
    {
        String dayStr = field.getVarValue("day");
        String monthStr = field.getVarValue("month");
        String yearStr = field.getVarValue("year");


        String dayStrValue = (dayStr!=null)?trim((String)PropertyUtils.getProperty(bean, dayStr)):"";
        String monthStrValue = (monthStr!=null)?trim((String)PropertyUtils.getProperty(bean, monthStr)):"";
        String yearStrValue = (yearStr!=null)?trim((String)PropertyUtils.getProperty(bean, yearStr)):"";

        String dt = dayStrValue + monthStrValue + yearStrValue;

        if(trim(dt).length() == 0)
        {

            returnValue = false;
        }

        if(!returnValue) 
        {
            errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
        }
    }
    catch (Exception e) 
    {
        returnValue = false;
        log.error(e,e);
        errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
    }

    return returnValue;

}

更新: 正如我在下面的评论中提到的那样。我试图在windows.onload()上调用javascript函数,如下所示,并根据条件禁用字段。但仍然出现验证错误。通过右侧,应忽略禁用的字段验证。如何在struts validation.xml中做到这一点?任何建议请。

   window.onload=function(){
   check();
    }

0 个答案:

没有答案