【问题标题】:in struts add button instead of submit form在struts中添加按钮而不是提交表单
【发布时间】:2015-07-06 11:28:42
【问题描述】:

如何通过单击按钮提交表单以调用 javascript 调用中的函数以及下面给出的所有表单信息。 我正在使用 struts 核心标签库并添加按钮标签。 下面是代码

<body>
    <html:form action="createtableAction.do?param=uploadExcelFile1" method="post" enctype="multipart/form-data">
        <table>
            <tr>
                <td align="left" colspan="1" class="migrate">
                    <html:file property="filename" styleId="filename" style="background-color:lightgoldenrodyellow; color:black;" />
                </td>
            </tr>
            <tr style="width:100%">
                <td align="center" colspan="2" class="migrate">
                    <html:submit onclick="return Checkfiles()" style="background-color:silver; color:darkblue;">Upload File</html:submit>
                </td>
            </tr>
        </table>
    </html:form>
</body>

在函数调用动作中并提交

function Checkfiles() {
    if (fileList[count] === tempFileName) {
        //want action and form submit code ...
        //document.forms[0].action='/createtableAction.do?param=uploadExcelFile1';
        //document.forms[0].submit();
        return true;
    } else {
        return false;
    }    
}

【问题讨论】:

  • 那么您是在问如何通过 Ajax 提交包含文件的表单?

标签: javascript struts el tld


【解决方案1】:

您可以使用 ajax 请求发送文件。 javascript中有FormData()available。您可以将文件附加到 FormData 对象并将其发送到 servlet。

function checkFiles(){
     var formData=new FormData();
     var uploadUrl = 'createtableAction.do?param=uploadExcelFile1';
     // Uncomment below this if you wish to send any other fields with file
     // formData.append('id', 1);
     console.log('loading file');
     var file= document.getElementById("filename"); 
     formData.append('filename',file);

     //create the ajax request (traditional way)
     var request = new XMLHttpRequest();
     request.open('POST', uploadUrl);
     request.send(formData);
     // if you want to reload uncomment the line below
     // window.location.reload();
}

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 2017-04-26
    • 2016-01-11
    • 2016-10-01
    • 1970-01-01
    相关资源
    最近更新 更多