简体   繁体   English

多个按钮调用相同的函数并执行不同的方法

[英]Multiple button calling same function and execute different methods

I have two buttons calling the same function: btnUpdate has to execute this function, whereas btnSave has no need to execute this method: 我有两个调用相同功能的按钮: btnUpdate必须执行此功能,而btnSave无需执行此方法:

function Update_New() {
  if (document.getElementById("btnUpdate").value == 2) {
    var update_confrimation = confirm("Are you sure you want to Update?");
    if (update_confrimation == true) {
      return true;
    } else {
      return false;
    }
  }
}

Code for the buttons: 按钮的代码:

<asp:Button ID="btnSave" runat="server" Text="SAVE" OnClick="btnSave_Click" OnClientClick="return Update_New(event)" value="1" />
<asp:Button ID="btnUpdate" runat="server" Text="UPDATE" OnClick="btnUpdate_Click" OnClientClick="return Update_New(event) " value="2" />

My Problem: When I click the update button, the function is not working. 我的问题:单击更新按钮时,该功能不起作用。

You need to pass the reference to the clicked button to the method 您需要将对单击按钮的引用传递给方法

<asp:Button ID="btnSave" runat="server" Text="SAVE" OnClick="btnSave_Click" OnClientClick="return Update_New(this)" value="1" />
<asp:Button ID="btnUpdate" runat="server" Text="UPDATE" OnClick="btnUpdate_Click" OnClientClick="return Update_New(this) " value="2" />

then using the clicked button's value you can show different messages if you want 然后使用点击按钮的值,您可以显示不同的消息

function Update_New(el) {
    return confirm(el.value == 2 ? "Are you sure you want to Update?" : 'Are you sure you want to save');
}
<asp:Button ID="btnSave" runat="server" Text="SAVE" OnClick="btnSave_Click" OnClientClick="return Update_New(this)" value="1" />
<asp:Button ID="btnUpdate" runat="server" Text="UPDATE" OnClick="btnUpdate_Click" OnClientClick="return Update_New(this) " value="2" />

just use "this" key instead of "event" and then in javascript use variable to store object( i have used variable X ). 只需使用“ this”键代替“ event”,然后在javascript中使用变量存储对象(我已经使用了变量X)。

     function Update_New(this) {
              if (this.value == 2) 
                    {
                    var update_confrimation = confirm("Are you sure you want to Update?");
                    if (update_confrimation == true) {
                        return true;
                    }
                    else {

                        return false;
                          }
                  }

}

You have a problem with your first if statement. 您的第一个if语句有问题。 Your button's text after render will be value so your if statement should be like this: 渲染后按钮的文本将很有value因此您的if语句应如下所示:

if (document.getElementById("btnUpdate").value === "UPDATE")

Also you have missed one } at the end of your code and also it is better using === instead of == . 另外,您在代码末尾错过了一个} ,并且最好使用===而不是== So your code should be like this: 因此,您的代码应如下所示:

function Update_New() {
        if (document.getElementById("btnUpdate").value === "UPDATE") {
            var update_confrimation = confirm("Are you sure you want to Update?");
            if (update_confrimation === true) {
                return true;
            } else {

            return false;
        }
    }
}

Please Note : Not all code paths return a value so consider adding one more return statement at the end. 请注意 :并非所有代码路径都返回值,因此请考虑在末尾再添加一个return语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM