【问题标题】:Button needs to be clicked twice to trigger function按钮需要点击两次才能触发功能
【发布时间】:2009-02-12 16:52:12
【问题描述】:

我知道这很难理解,但请试一试。 看看截图吧。

小输入框的名字是murl

add() 用于提交表单。如果murl 为空,则必须直接提交表单,如果它不为空,则必须根据数据库检查murl 条目(如果存在)。如果不存在则调用add()

问题是button必须点击两次才能触发该功能。

按钮上的代码是:

<button type="button" value="My button value" onclick="javascript: niju();" name="microsubmit" id="microsubmit">button</button> 

该按钮调用的 JavaScript 是:

function niju()
{
    var flag=1;
    var micro=document.getElementById('murl').value;

    $('#microsubmit').click(function()
    {

        if(micro=="")
        {
            add();
        }
        else
        {
            //remove all the class add the messagebox classes and start fading
            $("#msgbox")
                .removeClass()
                .addClass('messagebox')
                .text('Checking...')
                .fadeIn("slow");

            //check the username exists or not from ajax
            $.post("<?php echo SITE_ROOT;?>inc/user_availability.php",
                { murl: $("input:murl").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);

                            flag=0;
                        });
                    }
                    else
                    {
                        $("#msgbox")
                            //start fading the messagebox
                            .fadeTo(200,0.1,function()
                            {
                                //add message and change the class of the box and start fading
                                $(this)
                                    .html('Username available to register')
                                    .addClass('messageboxok')
                                    .fadeTo(900,1);   

                                flag=1;
                                add();
                            });
                    }
                });
        }
    });

    if(micro=="" && flag==1)
    {
        add();
    }
}

截图:

【问题讨论】:

  • 修复编辑新月的扎实工作

标签: javascript onclick


【解决方案1】:

它必须被点击两次,因为您在函数内部定义了#microsubmitclick 事件。因此,第一次单击时绑定事件处理程序,第二次事件处理程序就位并被触发。我没有详细讨论您要完成的工作背后的逻辑,但我的猜测是,如果您将事件绑定器移到函数之外并确保所有变量都在正确的范围内,那么它将起作用。

【讨论】:

    【解决方案2】:

    第一次加载页面时,单击处理程序不会与按钮挂钩,只有在您第一次单击按钮时才会调用 niju() 并挂钩单击事件。你需要做类似的事情

    $(document).ready() { 
       niju();
    }
    

    并从按钮声明中删除 onclick

    【讨论】:

      【解决方案3】:

      将你的标志移出函数 niju。

       var flag=1;
      function niju()
      {
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-23
        • 2023-03-17
        • 2010-12-04
        • 1970-01-01
        • 2014-08-08
        • 1970-01-01
        相关资源
        最近更新 更多