使用 javascript 在上传文件时执行两个操作。文件和文件详细信息不会传递到 javascript 函数

发布于 2024-12-08 19:34:14 字数 3488 浏览 0 评论 0原文

我正在使用此代码上传文件。我必须执行两个提交操作。所以我调用了一个 javascript 函数 onclick()。但是,文件和使用表单输入的文件详细信息都不会传输到 javascript 函数。我在这里缺少什么?

<jsp:include page="template-top.jsp" />
<script type="text/javascript">
function onSubmit(){
    document.forms["frm"].action="http://192.168.1.53/api.php/submit.xml";
    document.forms["frm"].target="_blank";
    document.forms["frm"].submit();
    document.forms["frm"].action="directuploadsubmit.do";
    document.forms["frm"].target="_blank";
    document.forms["frm"].submit();
}
</script>
<style type="text/css">

td.boldtext{
font-weight: bold;
font-family:Helvetica,sans-serif;
font-size:16pt;
color:black;
width:200;
}

</style>
<%@ page import="databeans.Submit" %>
<%@ page import="databeans.User"  %>
<%@ page import="databeans.Sandboxes"  %>

<%
    User user = (User) session.getAttribute("user");
%>

<div style="visibility:hidden">
<iframe NAME = "iframe1" WIDTH="40" HEIGHT="40"></iframe>
</div>

<form id="frm" name="frm" method="POST" enctype="multipart/form-data">
<table>
    <tr> <td class="boldtext">
    Direct Submission
    </td>
    </tr>
</table>
<table>
            <tr>
            <td colspan="2">
            <font color="red">Complete notes as Case#:Notes (Example: <font color="blue">"Case12345:test sample"</font>)
            </font>
            </td>
            </tr>
            <tr>
            <td width=50>
            <font color="black" face="Helvetica,sans-serif">File:* </font></td>
            <td><input type="file" name="file" value="${filename}" size="100"/></td>
            </tr>
            <tr>
            <td width=50>
            </td>
            <td><input type="hidden" name="email" readonly="readonly" value="<%=user.getUserName() %>" size="100"/></td>
            </tr>
            <tr>
            <td width=50>
            <font color="black" face="Helvetica,sans-serif">Notes: </font></td>
            <td><input type="text" name="notes" value="" size="100"/></td>
            </tr>
            <tr>
            <td width=50>
            <font color="black" face="Helvetica,sans-serif">Sandbox: </font></td>
            <td><select name="sandboxes[]">
            <option value="00-0C-29-CF-B8-A6">VMSB1 - Win7</option>
            <option value="00-0C-29-0A-AB-9A">VMSB2 - WinXP</option>
            </select>
            </td>
            </tr>
            <tr>
            <td width=50 colspan="2" align="left"><font face="Helvetica,sans-serif" color="FF0000">All * marked fields are mandatory    
            </font>
            </td>
            </tr>
            <tr>
            <td width=50 colspan="2" align="center">
        <!--    <input type="submit" name="submit" value="Submit" onclick= "this.form.target='_blank'; return true;">
        -->
        <input type="submit" name="button" value="Submit" onClick="onSubmit()">
            </td>
            </tr>
</table>
</form>
<jsp:include page="template-bottom.jsp" />

I am using this code to upload a file. I have to perform two submit actions. So I call a javascript function onclick(). However, neither the file nor the file details entered using the form are transferred to the javascript function. What am I missing here?

<jsp:include page="template-top.jsp" />
<script type="text/javascript">
function onSubmit(){
    document.forms["frm"].action="http://192.168.1.53/api.php/submit.xml";
    document.forms["frm"].target="_blank";
    document.forms["frm"].submit();
    document.forms["frm"].action="directuploadsubmit.do";
    document.forms["frm"].target="_blank";
    document.forms["frm"].submit();
}
</script>
<style type="text/css">

td.boldtext{
font-weight: bold;
font-family:Helvetica,sans-serif;
font-size:16pt;
color:black;
width:200;
}

</style>
<%@ page import="databeans.Submit" %>
<%@ page import="databeans.User"  %>
<%@ page import="databeans.Sandboxes"  %>

<%
    User user = (User) session.getAttribute("user");
%>

<div style="visibility:hidden">
<iframe NAME = "iframe1" WIDTH="40" HEIGHT="40"></iframe>
</div>

<form id="frm" name="frm" method="POST" enctype="multipart/form-data">
<table>
    <tr> <td class="boldtext">
    Direct Submission
    </td>
    </tr>
</table>
<table>
            <tr>
            <td colspan="2">
            <font color="red">Complete notes as Case#:Notes (Example: <font color="blue">"Case12345:test sample"</font>)
            </font>
            </td>
            </tr>
            <tr>
            <td width=50>
            <font color="black" face="Helvetica,sans-serif">File:* </font></td>
            <td><input type="file" name="file" value="${filename}" size="100"/></td>
            </tr>
            <tr>
            <td width=50>
            </td>
            <td><input type="hidden" name="email" readonly="readonly" value="<%=user.getUserName() %>" size="100"/></td>
            </tr>
            <tr>
            <td width=50>
            <font color="black" face="Helvetica,sans-serif">Notes: </font></td>
            <td><input type="text" name="notes" value="" size="100"/></td>
            </tr>
            <tr>
            <td width=50>
            <font color="black" face="Helvetica,sans-serif">Sandbox: </font></td>
            <td><select name="sandboxes[]">
            <option value="00-0C-29-CF-B8-A6">VMSB1 - Win7</option>
            <option value="00-0C-29-0A-AB-9A">VMSB2 - WinXP</option>
            </select>
            </td>
            </tr>
            <tr>
            <td width=50 colspan="2" align="left"><font face="Helvetica,sans-serif" color="FF0000">All * marked fields are mandatory    
            </font>
            </td>
            </tr>
            <tr>
            <td width=50 colspan="2" align="center">
        <!--    <input type="submit" name="submit" value="Submit" onclick= "this.form.target='_blank'; return true;">
        -->
        <input type="submit" name="button" value="Submit" onClick="onSubmit()">
            </td>
            </tr>
</table>
</form>
<jsp:include page="template-bottom.jsp" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎歌 2024-12-15 19:34:14

您应该将 onSubmit 函数绑定到表单,而不是 submit 按钮。

<script type="text/javascript">
function onSubmit(){
    //First form submission:
    document.forms["frm"].action = "http://192.168.1.53/api.php/submit.xml";
    document.forms["frm"].submit(); //Submit #1

    //Change the action
    document.forms["frm"].action = "directuploadsubmit.do";
}
</script>
...

<form action="directuploadsubmit.do" onsubmit="onSubmit();" name="frm" method="POST" enctype="multipart/form-data" target="_blank">
... rest of page

当用户禁用 JavaScript 时,默认操作是仅在新窗口中将表单提交到 directuploadsubmit.do。启用 JavaScript 后,会发生以下情况:

  1. action 属性更改为 submit.xml
  2. 表单提交(使用 .submit()
  3. action 属性被更改(回到 direcuploadsubmit.do
  4. 表单提交,因为用户点击了提交按钮。[1]

[1] 当用户单击提交时,会发生以下事件:

  1. 触发 submit 事件(如果JavaScript 已启用)
  2. 表单已提交到 action 中提到的页面。

You should bind the onSubmit function to the form, instead of the submit button.

<script type="text/javascript">
function onSubmit(){
    //First form submission:
    document.forms["frm"].action = "http://192.168.1.53/api.php/submit.xml";
    document.forms["frm"].submit(); //Submit #1

    //Change the action
    document.forms["frm"].action = "directuploadsubmit.do";
}
</script>
...

<form action="directuploadsubmit.do" onsubmit="onSubmit();" name="frm" method="POST" enctype="multipart/form-data" target="_blank">
... rest of page

When the user has disabled JavaScript, the default action is to only submit the form to directuploadsubmit.do, in a new window. When JavaScript is enabled, the following happens:

  1. The action attribute is changed, to submit.xml.
  2. The form submits (using .submit()
  3. The action attribute is changed (back to direcuploadsubmit.do
  4. The form submits, because the user clicked at the submit button.[1]

[1] When an user clicks at submit, the following events occur:

  1. The submit event is triggered (if JavaScript is enabled)
  2. The form is submitted to the page as mentioned at action.
七婞 2024-12-15 19:34:14

您必须通过调用 onclick="onSubmit(); return false;" 来阻止表单或提交按钮的默认操作

这不会让您的表单被提交并允许您提前处理请求

you must prevent the default action of either the form or the submit button, by calling onclick="onSubmit(); return false;"

that won't let your form to get submited and allow you to handle the request beforehand

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文