对特定表单执行 jquery 提交功能

Execute jquery submit function for a specific form

我正在开发一个具有 4 个导航选项卡的 Web 应用程序。

名为 index.cshtml 的主 cshtml 文件包含 4 个导航选项卡作为部分视图:

<div class="tab-content" id="mainTab" >
        <div class="tab-pane fade in active" id="@ViewData["Stamp"]" role="tabpanel" aria-labelledby="stempel-tab">
          @{ViewBag.Title = "Index";}
          @{Html.RenderAction("_Stempel", "Home");}
        </div>
        <div class="tab-pane fade" id="@ViewData["BackDated"]" role="tabpanel" aria-labelledby="backdated-tab">
          @{ViewBag.Title = "Index";}
           @{Html.RenderAction("_BackDated", "Home");}
        </div>
        <div class="tab-pane fade" id="@ViewData["History"]" role="tabpanel" aria-labelledby="historie-tab">
           @{ViewBag.Title = "Index";}
           @{Html.RenderAction("_Historie", "Home");}
        </div>
        <div class="tab-pane fade" id="@ViewData["ChangePin"]" role="tabpanel" aria-labelledby="changepin-tab">
           @{ViewBag.Title = "Index";}
           @{Html.RenderAction("_Pinaendern", "Home");}          
        </div>
    </div>  

每个选项卡都有一个表单,我必须在其中提交信息。我使用 Html.BeginForm 来定义表格:

@using (Html.BeginForm("SavePost", "Home", FormMethod.Post))

为了执行表单,我使用了一个按钮(每个选项卡都有一个不同的按钮):

<div class="stempel_button" id="my_centered_buttons">                              
   <button id="stempel" value="stempel" type="submit" style="width:200px"class="btn btn-primary" >@ViewData["Button_Booking"]</button>                    
</div>

按下按钮时调用函数 SavePost:

    $(document).ready(function () {     


            //Executed when pressed send/book
            $('#stempel').click(function () {

                alert("hello click stempel");

                $("form[action$='SavePost']").submit(function () {

                //Some Action
            }
     }
}

函数 SavePost 定义在 HomeController.cs:

[AcceptVerbs(HttpVerbs.Post)]       
        public String SavePost()
        { 
          // Some code
        }

当我在一个选项卡中单击提交按钮时,它会在所有其他选项卡中执行此行:

$("form[action$='SavePost']").submit(function () {

        //Some Action
  }

是否可以为每个表单定义一个ID,然后用一个ID调用提交函数,这样就只执行特定的表单?

像这样:

@using (Html.BeginForm("SavePost", "Home", FormMethod.Post, new { id = "stempelform" }))

感谢您的回答...

使用 $("form[action$='SavePost']").eq(0).submit();每个表格都有效。

如果您使用这样的按钮条件:

$("#buttonid").click(function () {
}

它会在您每次单击该按钮时添加一个提交。我只是删除了条件并且它起作用了。

谢谢!