【问题标题】:alert message on button click and text changes in the fields单击按钮时的警报消息和字段中的文本更改
【发布时间】:2015-08-13 06:24:44
【问题描述】:

我有一个场景要解决。我有 2 个用于名字、姓氏的文本框和一个用于保存这些字段的按钮。在页面加载时,我们正在填充 那些带有数据库中一些数据的文本字段,当我们更改任何这些文本字段中的文本并单击“保存”按钮时,我们需要显示一条消息/警报,例如

"You're about change the name of the person, john, smith to Daniel, clark". Do you wanna proceed? 

如果用户以“是”响应消息/警报,那么只有我们应该允许保存这些更改。

代码片段:

<asp:TextBox ID="txtfirstName"  runat="server" required="true" caption="First Name:" ></asp:TextBox>                           
               <asp:TextBox ID="txtlastName"  runat="server" caption="Last Name:"></asp:TextBox>

 <asp:Button ID="btnSave" runat="server"  Text="Save" CausesValidation="False"  />&nbsp;

后面:

只有在用户选择更改名称后,我们才需要在后面调用这段代码

 Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

如果文本字段相同,我们不需要显示任何确认消息。 你能告诉我如何处理这个吗?

【问题讨论】:

  • 你不要调用这样的事件,创建另一个包含你的保存代码的方法,然后调用它,而不是按钮单击事件。
  • 试试这个
  • 在显示消息之前,您是否需要重新查询数据库以确认名称已更改?还是可以将原始值放入隐藏字段中,然后根据弹出窗口的显示比较新旧值?
  • 我可以将这些名称放入隐藏字段中进行比较.. @EJD
  • 抱歉没有早点回复你

标签: javascript jquery vb.net textbox alert


【解决方案1】:

这应该可以满足您的需求:)

JavaScript 已格式化,因此您可以轻松阅读。您通常会将 JavaScript 放在 1 行,或者您可以创建一个函数并调用该函数。

还可以查看confirm() JavaScript,您可以为弹出窗口提供适当的标题,它们是其他选项。

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HiddenField ID="hdnfirstName" runat="server" Value="name" />
        <asp:HiddenField ID="hdnlastName" runat="server" Value="last" />
        <asp:TextBox ID="txtfirstName" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtlastName" runat="server"></asp:TextBox>
        <asp:Button ID="btnSave" runat="server" Text="Save" runat="server" 
        OnClientClick="if(document.getElementById('hdnfirstName').value !== document.getElementById('txtfirstName').value || 
                      document.getElementById('hdnlastName').value !== document.getElementById('txtlastName').value){

                            if (confirm('You\'re about change the name of the ' + 
                                         document.getElementById('hdnlastName').value + ', ' + 
                                         document.getElementById('hdnfirstName').value + ' to ' + 
                                         document.getElementById('txtlastName').value + ', ' + 
                                         document.getElementById('txtfirstName').value + 
                                         '. Do you wanna proceed?')) 
                            {
                                return true; 
                            } else { 
                                return false; 
                            }
                      } else {
                        return true;
                      }" />
    </div>
    </form>
</body>
</html>

【讨论】:

  • 确认弹出窗口也可以替换为 JQuery 弹出窗口,因为有太多可供选择的答案太宽泛而无法回答。你有框架。
  • 我为这个答案的问题写了另一个问题。请找到这个 [stackoverflow.com/questions/32081136/…
猜你喜欢
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 2021-08-06
相关资源
最近更新 更多