0

我正在开发一个 ASP.Net MVC 应用程序,并且我的表单包含一个文件上传 HTML 元素和一个提交按钮。当用户第一次进入视图时,我有 javascript 代码自动触发文件上传 HTML 元素上的单击事件,因此用户不必自己按下按钮。这是为了方便和我想要的方式,但是当我自动触发按钮单击时,它会导致以下问题:我必须按两次提交按钮才能提交表单。第一次按下清除文件上传 HTML 元素中的文本,第二次按下提交表单。谁能解释为什么会发生这种情况以及我怎样才能让它不这样做?

注意:表单明确设置为不使用 Ajax,因为我正在上传图像,据我了解,您无法通过 Ajax 上传图像,因此我在表单 HTML 属性中使用 data-ajax = "false" 将其关闭。将此设置为 false 与我遇到的问题有关。如果我通过 ajax 提交,我只需要按一次提交按钮。如果我不使用ajax,我需要按两次。

这是显示问题的基本代码:

@Using Html.BeginForm("Document", "NewDocument", Model, FormMethod.Post, New With {.enctype = "multipart/form-data", .data_ajax = "false", .id="submitimage-form"})

@<input type="file" id="fileInput" name="fileInput" style="visibility:hidden;" />   

@<input type="submit" id="accept2" value="Accept" data-mini="true" data-theme="b" data-icon="check"/>
End Using

<script>

// Do once after DOM is loaded for the page
$(document).bind("pageinit", function () {
    $("#fileInput").click();
});

</script>
4

0 回答 0