2014-06-15 53 views
0

(我使用ASP.NET,MVC 4,C#,引導,使用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(); 
}); 

});

是冷藏本節在我看來(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屬性它的作品很棒...我怎樣才能讓提交按鈕觸發兩個事件?

一個事件是顯示處理文件的警報。 另一個事件是將文件發佈到控制器中的方法。

我該怎麼辦?如果可能的...

我到底是什麼了做:(IMoses答案)

我剛剛從腳本刪除 「的preventDefault」。

+1

只需從事件處理程序中移除'e.preventDefault();'。您正在積極停止提交表單。 – iMoses

+0

Mohse它的作品,當我刪除「preventDefualt」我在這裏失去了什麼? – Ron

+1

不會。會發生的事情是您的事件處理程序將被執行,一旦完成它將繼續冒泡'click'事件,直到觸發'submit'動作。節省您自己啓動'submit'的麻煩。 – iMoses

回答

0

問題是,防止默認的調用將阻止表單提交。所以添加一個調用來提交你的表單將解決這個問題。

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

表單仍然沒有提交...我嘗試甚至改變這樣的行的順序: $('form:first')。submit(); $('#processAlert')。slideDown(); 它仍然只是顯示警報沒有更多。 – Ron