表格提交问题


                    

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

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

以下是显示问题的基本代码:</ p>

  @使用Html.BeginForm(“ Document”,“ NewDocument”,Model,FormMethod.Post,{。enctype =“ multipart / form-data”,.data_ajax =“ false”,.id =  “ submitimage-form”})

@

@
最终使用

<脚本>

//为页面加载DOM后执行一次
$(document).bind(“ pageinit”,function(){
     $(“#fileInput”)。click();
});

</ script>
</ code> </ pre>
     </ div>

展开原文

原文

I am developing an ASP.Net MVC application and I have form containing a file upload HTML element and a submit button. When the user first goes to the view, I have javascript code automatically triggering the click event on the file upload HTML element so the user does not have to press the button themselves. This is for convenience and the way I want it to be, but when I trigger the button click automatically it is causing the following issue: I have to press the Submit button twice in order to submit the form. The first press clears the text in the file upload HTML element and the second press submits the form. Can anyone explain why this is happening and how I can get it not to do this?

Note: The form is explicitly set not to use Ajax because I am uploading an image and from my understanding you cannot upload images via Ajax, so I turn that off with data-ajax = "false" in my form HTML attributes. Having this set to false is related to the issue I am having. If I submit via ajax, I only have to press the submit button once. If I do not use ajax, I need to press twice.

Here is essential code showing issue:

@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>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问