2013-10-26 26 views
0

我有一個網頁有兩種形式。jquery如何檢測mvc4中的表單ID

這樣的:

<div class="ApartmentOwnerRegister"> 
@using (Html.BeginForm("RegisterApartmentOwner", "Home", FormMethod.Post, 
          new { enctype = "multipart/form-data" })) 
          { 
    <p> 
     <label>First Name</label> 
     <input type="text" placeholder="Enter your first Name" /> 
     <span class="errorMessage"></span> 
    </p> 
    <p> 
     <label>Last Name</label> 
     <input type="text" placeholder="Enter your last Name" /> 
     <span class="errorMessage"></span> 
    </p> 
    <p> 
     <label>Mobile Number</label> 
     <input type="text" placeholder="Enter your mobile number" /> 
     <span class="errorMessage"></span> 
    </p> 
    <p> 
     <input type="submit" value="Register" class="submit"/> 
    </p> 
    } 
</div> 
<div class="TenantRegister"> 
@using (Html.BeginForm("RegisterTenant", "Home", FormMethod.Post, 
          new { enctype = "multipart/form-data" })) 
          { 
    <p> 
     <label>First Name</label> 
     <input type="text" placeholder="Enter your first Name" /> 
     <span class="errorMessage"></span> 
    </p> 
    <p> 
     <label>Last Name</label> 
     <input type="text" placeholder="Enter your last Name" /> 
     <span class="errorMessage"></span> 
    </p> 
    <p> 
     <label>Mobile Number</label> 
     <input type="text" placeholder="Enter your mobile number" /> 
     <span class="errorMessage"></span> 
    </p> 
    <p> 
     <label>Passport Number</label> 
     <input type="text" placeholder="Enter your passport number" /> 
     <span class="errorMessage"></span> 
    </p> 

    <p> 
     <label for="file">Upload You Passport:</label> 
     <input type="file" name="file" id="passport" style="width: 100%;" /> 
     <span class="errorMessage"></span> 
    </p> 
    <p> 
     <label for="file">Upload You Image:</label> 
     <input type="file" name="file" id="image" style="width: 100%;" /> 
     <span class="errorMessage"></span> 
    </p> 
    <p> 
     <input type="submit" value="Register" class="submit"/> 
    </p> 
} 
    </div> 

用戶提交什麼形式,我想觸發一個jquery功能。

過去

,沒有MVC的,我這樣做:

$("#formID").on('submit',function (e){}); 

但在該代碼我應該怎麼做,而不是formID

回答

1

使用

$(document).ready(function() { 
    $(".TenantRegister form").on('submit', function (e) {}); 
}); 

參考下你的HTML在OP公司發佈的代碼

<div class="TenantRegister"> 
@using (Html.BeginForm("RegisterTenant", "Home", FormMethod.Post, 
          new { enctype = "multipart/form-data" })) 
          { 
+0

沒有文件.read? –

+0

@MarcoDinatsoli使用'$(document).ready(function(){'檢查更新後的答案。 –

+0

我得到了異常,因爲控制器還沒有存在,但是我在函數中做了三個警告語句,第二個斷點但是,沒有任何提醒,請問爲什麼呢? –

2

您可以使用ID您Html.BeginForm

@using (Html.BeginForm("RegisterTenant", "Home", FormMethod.Post, new { id = "formID", enctype = "multipart/form-data" })) 

和jQuery

$("#formID").on('submit',function (e){}); 

我想這是你正在尋找的那個