简体   繁体   English

jQuery Click事件在更改事件之前加载

[英]JQuery Click event loads before change event

I am using a simplified example to describe the issue I am facing. 我使用一个简化的示例来描述我面临的问题。

I have the following HTML markup: 我有以下HTML标记:

<input ng-model="something" style="margin-top:8px;"/>

And, I have two HTML buttons: 而且,我有两个HTML按钮:

<button id='submit'>Save</button>

<button id='btnGetAnalyzerInput'>Generate Analyzer File </button>

I used jQuery's "change" event on my input (to track whether any changes have been made to the input - by maintaining a simple JS variable). 我在输入中使用了jQuery的“ change”事件(通过维护一个简单的JS变量来跟踪是否对输入进行了任何更改)。

When the user clicks "Generate Analyzer file button", what I want to is this: 当用户单击“生成分析器文件按钮”时,我想要的是:

  1. Look up the JS variable to find out whether any changes have been made. 查找JS变量以查找是否进行了任何更改。
  2. If yes, then prompt the user to save changes (window.dialog) 如果是,则提示用户保存更改(window.dialog)

However, I find that when the focus is still on the input element, and when the button is clicked, the click event runs before the OnChange event. 但是,我发现当焦点仍然位于输入元素上并且单击按钮时,单击事件在OnChange事件之前运行。 In all other cases, it is the OnChange event which gets fired before the click event (and so my code works as expected). 在所有其他情况下,都是OnChange事件在click事件之前触发(因此我的代码按预期工作)。

Is there any way to ensure that for such a scenario, the click event runs after the onChange event? 有什么方法可以确保在这种情况下,单击事件在onChange事件之后运行?

I am using Google Chrome to test my application. 我正在使用Google Chrome浏览器测试我的应用程序。

Note : 注意 :

  1. Both events work as expected - the OnChange event gets fired when the textbox loses focus. 这两个事件均按预期方式工作-当文本框失去焦点时,将触发OnChange事件。
  2. I cant use the keypress event since I want to track changes. 由于要跟踪更改,因此无法使用keypress事件。

You could have the click event call the same function as the OnChange event. 您可以使click事件调用与OnChange事件相同的函数。 Something like this: 像这样:

function OnChange(){
  //Do stuff for on change;
}
function ClickEvent(){
  OnChange(); 
  //continue with generate stuff
}

You you may need to set up and pass in arguments to the OnChange function, depending on how you are accessing the data you need. 您可能需要设置参数并将其传递给OnChange函数,具体取决于您访问所需数据的方式。 If you need more guidance, post more of your code. 如果您需要更多指导,请发布更多代码。

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

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