禁用freemarker / spring表单上的提交按钮,直到选择文件

时间:2011-12-14 15:46:14

标签: spring spring-mvc freemarker

在用户选择文件之前,如何禁用提交按钮?

我的页面上有一个表单:

<table>
    <form method="post" enctype="multipart/form-data" id="messageupload">
    <@spring.bind "messageUploadCommand.*"/>
        <tr>
            <td><@spring.formInput 'messageUploadCommand.multipartFile' '' 'file' /></td>
            <td><button type="submit" id="searchButton">Upload</button></td>
            <td><@spring.formSingleSelect 
                    'messageUploadCommand.messageFormat', 
                    messageFormats, '' /> 
                <@spring.showErrors  '<br>', 'error' /> </td>
        </tr>
    </form>
</table>

如果用户在选择文件之前提交表单我

freemarker.template.TemplateException: Error executing macro: formSingleSelect required parameter: options is not specified. 
 at freemarker.core.Macro$Context.sanityCheck(Macro.java:211) 
 at freemarker.core.Macro$Context.runMacro(Macro.java:169) 
 at freemarker.core.Environment.visit(Environment.java:603) 
 at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106) 
 at freemarker.core.Environment.visit(Environment.java:210) 
 at freemarker.core.MixedContent.accept(MixedContent.java:92) 
 at freemarker.core.Environment.visit(Environment.java:210)
 at freemarker.core.Environment.process(Environment.java:190)
 at freemarker.template.Template.process(Template.java:237)
 at ...

1 个答案:

答案 0 :(得分:0)

我就这样做了:

<table>
    <form method="post" enctype="multipart/form-data" id="messageupload">
    <@spring.bind "messageUploadCommand.*"/>
        <tr>
            <#assign onChangeScript = "document.getElementById('uploadButton').disabled = (value.length=0); ">
            <td><@spring.formInput 'messageUploadCommand.multipartFile' 'onchange=\"${onChangeScript}\"' 'file' /></td>
            <td><button type="submit" disabled="true"
                          id="searchButton">Upload</button></td>

            <td><@spring.formSingleSelect 
                    'messageUploadCommand.messageFormat', 
                    messageFormats, '' /> 
                <@spring.showErrors  '<br>', 'error' /> </td>
        </tr>
    </form>
</table>

我通过Freemarker变量添加了文件选择器onchange脚本,默认情况下禁用了该按钮。 onchange脚本测试值的长度,即文件名,为零。