简体   繁体   English

如何在Javascript中获取HTML页面的活动元素

[英]How to get the active element of a HTML page in Javascript

I have a HTML page which has only one input text box and one button just after that. 我有一个HTML页面,其中只有一个输入文本框和一个按钮。 I have put onblur event handler for the input text box where I am calling an ajax call which does only validation and then shows the error. 我已经为输入文本框放了onblur事件处理程序,我在其中调用ajax调用,它只进行验证,然后显示错误。 Similarly, I have put onclick event on the button where I am again calling the different Ajax call which also does validation and if validation passes then it does some DB update also and then redirect to someother URL. 类似地,我在按钮上放了onclick事件,我再次调用不同的Ajax调用,它也进行验证,如果验证通过,那么它也会进行一些数据库更新,然后重定向到另一个URL。

My requirement is if the user has focus on the input box and then he simply clicks on the button(without loosing the focus) then only button ajax call should fire. 我的要求是,如果用户专注于输入框,然后他只需点击按钮(不会失去焦点),那么只有按钮ajax调用才会触发。 However, I am seeing two ajax calls one for onblur and one for onclick is firing. 但是,我看到两个ajax调用一个用于onblur,一个用于onclick正在触发。

I tried using event.relatedtarget in the onblur to see if it is generating on the click of button. 我尝试在onblur中使用event.relatedtarget来查看它是否在单击按钮时生成。 But it works only in chrome. 但它只适用于chrome。 I checked document.activeelement also. 我也检查了document.activeelement。 This also doesn't work. 这也行不通。

Please let us know how can we handle this in all the browser. 请告诉我们如何在所有浏览器中处理此问题。

Thanks, Sourabh 谢谢,Sourabh

You could set a global flag like this 你可以像这样设置一个全局标志

var submitting = false;
$('#button').click(function(){
    if (!submitting) {
        submitting = true;
        $.ajax({
           url: 'whateverurl.php',
           success: function(data) {
               submitting = false;
           }
        });
    }
});

This is sudo code since I don't know your source, you would do the same thing for your onblur 这是sudo代码,因为我不知道你的来源,你会为你的onblur做同样的事情

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

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