【问题标题】:How do I add a confirmation popup on a button (GET POST action in MVC)?如何在按钮上添加确认弹出窗口(MVC 中的 GET POST 操作)?
【发布时间】:2025-12-17 16:55:02
【问题描述】:

我在一个 aspx 页面上有一个 get/post/JSON 函数。此页面将在文本框中输入的数据添加到由 javascript 填充的表中。当用户选择提交按钮时。如果文本框不为空,则有一个弹出按钮告诉用户文本框中的数据未保存在表格中。如何在控制器中的发布操作上显示确认“确定/取消”弹出窗口? 我快速总结了我的代码是什么样的。

...
<%  using (Html.BeginForm("AddName", "Name", FormMethod.Post, new { id = "AddNameForm" })) { %>
...
<table id="displayNameTable" width= "100%">                        
    <tr>
        <th colspan="3">Names Already Added</th>
    </tr>
    <tr>
        <td style="font-size:smaller;" class="name"></td>
    </tr>
</table>  
...
<input name="Name" id="txtInjuryName" type="text" value="<%=test.Name %>" />
...
<input type="submit" name="add" value="Add"/>
<% } %>
<form id="form1" runat="server">
           string confirmNext = "";
    if (test.Name == "")
    {
       confirmNext = "return confirm('It seems you have a name not added.\n\nAre Continue?')";
    }%>
    <input type="submit" name="getNext" value="Next" onclick="<%=confirmNext%>" />
</form>

【问题讨论】:

    标签: html asp.net-mvc model-view-controller asp.net-mvc-2


    【解决方案1】:
    <script type="text/javascript">
    
        $(document).ready(function(){ $("#form1").bind("submit", function(){ 
             if(!confirm('It seems you have a name not added.\n\nAre Continue?')) 
                   return false;   
             });}          
         });
    
    </script>
    

    【讨论】:

    • +1 用于使用 jQuery 将客户端交互与 html 元素分开。有助于可维护性。
    • 这是一个好方法...但是我如何检查文本框是否为空???
    • 添加 if($("#txtInjuryName").val()==""){...}
    • 谢谢...我是 jquery 用法的新手。
    【解决方案2】:

    您可以使用表单的onsubmit 事件:

    <form id="form1" onsubmit="return confirm('It seems you have a name not added.\n\nAre Continue?')">
        <input type="submit" name="getNext" value="Next" />
    </form>
    

    为什么还要在 ASP.NET MVC 应用程序中使用runat="server"?您还可以考虑指定此表单要发布到的action,并使用Html.BeginForm 助手作为您的第一个表单。

    【讨论】:

      最近更新 更多