简体   繁体   English

测试是否单击了按钮

[英]Testing if a button has been clicked or not

I have an asp.net page which loads and writes values from a record to the controls on the page. 我有一个asp.net页面,该页面将记录中的值加载并写入页面上的控件。

There is a link to another page and a save button. 有一个链接到另一个页面和一个保存按钮。

If the user clicks save, fine, the changes are saved and they can navigate to wherever. 如果用户单击保存,就可以保存更改,并且他们可以导航到任何地方。

If they dont click save and click the link, any changes are lost. 如果他们不单击保存,然后单击链接,则所有更改都将丢失。

Is there a way to check if the save has been clicked while the user has that page loaded? 有没有一种方法可以检查在用户加载该页面时是否单击了保存?

I'm thinking along the lines of an OnClick Javascript function for the link such as -below, I'm not sure though how to test for the save button being clicked - can anyone point me in the right direction? 我沿着一个onClick JavaScript函数的行思考的链接,如-below,我不知道但如何测试被点击保存按钮 - 任何人都可以点我在正确的方向?

function handleHyperLinkClick(hyperlink) 
{ 
    confirm("You have not clicked save, any changes will be lost - click cancel then save  your changes or ok to continue");
 } 

Add a bool variable, that is default false, set to true if the button has been clicked. 添加一个bool变量,默认为false,如果单击该按钮,则设置为true。 Then check the value if user leaves the page. 然后,如果用户离开页面,请检查该值。

You can do using hidden field . 您可以使用hidden field when user click on save button then set hidden field value. 当用户单击保存按钮,然后设置hidden field值。

You have to keep a temporary variable in javascript like state; 您必须在javascript之类的状态下保留一个临时变量;

` `

var state = 0;

 //your function calls here
function yourFunction()
{
//// your coding
 state =1;
//// your coding
}

` In this case, you have to submit the value or navigate to other page when state=1, otherwise you shouldnt. `在这种情况下,当state = 1时,您必须提交值或导航到其他页面,否则不应该。 This flag to be checked whilst clicking link (if its navigating via function) or onBeforeUnload event for page. 单击链接(如果通过功能导航)或页面的onBeforeUnload事件时,将检查此标志。

Please correct me know if you are looking for something other. 如果您正在寻找其他东西,请指正我。

You can attach some value to a variable when the button is clicked. 单击按钮时,可以将一些值附加到变量。 Then when the user tries to navigate, check whether that variable has that value. 然后,当用户尝试导航时,检查该变量是否具有该值。 If so, it has been clicked, else not: 如果是这样,则单击它,否则不会:

button = document.getElementById("button");
button.onclick = function() { clicked = true; }
document.getElementById("link").onclick = function() { if (!clicked) { alert("Sure?"); }

Assuming you are using ASP.NET server buttons like this: 假设您正在使用如下所示的ASP.NET服务器按钮:

<asp:Button runat="server" id="someButtonId" Click="someClickHandler" Text="Continue" />

If I were you, I would use jQuery to dynamically bind the click event of the button like this: 如果我是你,我将使用jQuery来动态绑定按钮的click事件,如下所示:

$(document).ready(function() {
   $("input[type=button][id$=someButtonId]").on("click", function(e) {
      if(!confirm("You have not clicked save, any changes will be lost - click cancel then save  your changes or ok to continue"))
      {
          e.preventDefault()
      }
   });
});

Hope this helps. 希望这可以帮助。

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

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