javascript - 帮助找出功能异常的原因

标签 javascript function

抱歉再次打扰大家。由于某种原因,当我尝试使用 validateusername() 运行以下命令时,没有任何反应。

<script language="javascript">
//<----------------------------------+
//  Developed by Roshan Bhattarai and modified by Harry Rickards
//  Visit http://roshanbh.com.np for the origional version of this script and more.
//  This notice MUST stay intact for legal use
// -------------------------->
function validateusername()
{
    $("#username").blur(function()
    {
        //remove all the class add the messagebox classes and start fading
        $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn(1000);
        //check the username exists or not from ajax
        $.post("usernamecheck.php",{ username:$(this).val() } ,function(data)
        {
          if(data=='no') //if username not avaiable
          {
            $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
            { 
              //add message and change the class of the box and start fading
              $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
            });     
          }
          else
          {
            $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
            { 
              //add message and change the class of the box and start fading
              $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);    
            });
          }

        });

    });
}
</script>

这可能是一个简单的错误,但是有人能够指出我正确的方向吗?

谢谢

最佳答案

代码没有语法错误,但是当您使用事件绑定(bind)时,您是否尝试在页面加载时运行它?我相信您看不到结果,因为相应的输入不会在所需的事件上运行您的函数。

$().ready(function(){
validateusername();
});

另外,使用<script type="text/javascript"> , language已弃用。

关于javascript - 帮助找出功能异常的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1862405/

相关文章:

javascript - jQuery Validate() 和 Valid() 方法未定义或不起作用

javascript - 使用变量进行全局替换

javascript - 如何从 $resource 获得空响应?

javascript - 如果输入为空,我该怎么做,什么都不会发生

c++ - 使用 QObject::connect() 将函数连接到信号时,类型签名是否重要?

javascript - 基本的 Javascript 作用域

javascript - 访问对象

function - (->) 的 Profunctor 实例同时定义 dimap 和 lmap/rmap 是否有任何原因?

c - 如何让这个C程序在不修改代码的情况下调用这个函数?

python - 在不使用正则表达式的情况下实现 python replace() 函数