简体   繁体   English

为什么在ajax或javascript函数调用之后输入字段被禁用?

[英]Why input fields get disabled after ajax or javascript function call?

I am making a simple signup form where I check whether the email users enters already exists or not. 我正在制作一个简单的注册表单,用于检查用户输入的电子邮件是否已经存在。 This is done via onChange function which in turn checks through ajax whether the email entered by the user is available or not. 这是通过onChange函数完成的,该函数依次通过ajax检查用户输入的电子邮件是否可用。 Currently I just show an alert box if the email is already registered. 目前,如果电子邮件已经注册,我只会显示一个警告框。 But I have noticed a strange behavior. 但是我注意到一个奇怪的行为。 After clicking the ok button in alert box the whole form gets disabled for text input. 单击警报框中的“确定”按钮后,整个表单将无法输入文本。 I cant enter the text into remaining fields and refreshing the page is the only solution to make the form active for input again. 我无法在其余字段中输入文本,刷新页面是使表单再次可用于输入的唯一解决方案。 Why this is so? 为什么会这样呢? And how do I prevent it? 以及如何预防呢?

here is my code: 这是我的代码:

One of my input field 我的输入字段之一

<input type="email" name="email" onChange="checkEmail()" id="email"/>

And here is the javascript and ajax code for checking email if it exists or not: 这是用于检查电子邮件是否存在的javascript和ajax代码:

       function checkEmail()
         {

            var email=$('#email').val();


            var dataString = 'email=' + email;              
            $.ajax({
                type: "POST",
                url: "<?=base_url();?>home/check_email",            
                data: dataString,                           
                dataType:"html",
                success: function(msg)
                    {
                        if(msg=="1")
                        {
                            alert("The email already exists in our records!");
                        }

                    }
            });
        }

您必须使用onBlur

<input type="email" name="email" onBlur="checkEmail()" id="email"/>

You should use onBlur, this event triggered when you leave the textbox. 您应该使用onBlur,该事件在您离开文本框时触发。 Otherwise it would be called every time a character in the textbox changes. 否则,每次文本框中的字符更改时都会调用它。

<input type="email" name="email" onBlur="checkEmail()" id="email"/>

或者如果您确实想检查每个按键(无论出于何种原因),请使用按键事件

 <input type="email" name="email" onKeyPress="checkEmail()" id="email"/>

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

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