我如何提交一个表单,并弹出一个警告在同一时间

How can i submit a form and pop up an alert at the same time?

本文关键字:一个 警告 同一时间 何提交 表单 提交      更新时间:2023-09-26

. NET, MVC 4, c#, Bootstrap, Javascript)

我有一个表单来上传文件(多个),它的作品很棒,这是表单:

            @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <table>
                <tr>
                    <td>Choose a filter:</td>
                    <td style="margin-bottom:auto">
                        @Html.DropDownList("filterProfile", (SelectList)ViewBag.FilterList)
                    </td>
                </tr>
                <tr>
                    <td>File:</td>
                    <td><input type="file" name="Files" id="Files" multiple /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="submit" value="Filter" /></td>
                </tr>
            </table>
        }

因为上传文件可能会花费一些时间,所以我想在表单中添加一个提示我们正在处理你的文件的弹出警告会很好,所以我在提交按钮中添加了一个ID,如下所示:

 <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="submit" id="alertMe" value="Filter" /></td>
                </tr>
            </table>

你可以看到ID是'alertMe'。

然后我在JS文件中写了这段代码:

$(function () {
$('#alertMe').click(function (e) {
    e.preventDefault();
    $('#processAlert').slideDown();
});

});

指的是my View(HTML)中的这一部分:

<div class="alert alert-info alert-dismissable" id="processAlert">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true" id="processAlert">&times;</button>
<strong>Processing...</strong> Upload files and Processing your request.

但是现在它只显示警报而不提交文件,如果我取下提交按钮的ID属性,它的工作很棒…如何使提交按钮触发两个事件?

一个事件是显示处理文件的警报。另一个事件是将文件发送到控制器中的方法。

我该怎么做呢?如果可能的话……

我最后做了什么:(immoses Answer)

我只是从脚本中删除了"preventDefault"

问题是调用prevent default会阻止表单提交。因此,添加一个调用来提交您的表单将解决这个问题。

$(function () {
$('#alertMe').click(function (e) {
   e.preventDefault();
   $('#processAlert').slideDown();
   $('form:first').submit();
});