需要单击两次按钮才能触发功能 [英] Button needs to be clicked twice to trigger function

查看:160
本文介绍了需要单击两次按钮才能触发功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这很难理解,但请尝试一下. 看看屏幕截图.

I know this would be difficult to understand but please give a try. Have a look at the screenshot.

小输入框的名称是murl.

add()用于提交表单.如果murl为空,则必须直接提交表单;如果其不为空,则必须对照数据库检查murl条目(如果存在).如果不存在,则调用add().

add() is used to submit the form. if murl is empty the form has to be submitted directly, if its not empty the murl entry has to be checked against the database if it exists. if it doesn't exist add() is called.

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

The problem is the button has to be clicked twice to trigger the function.

按钮上的代码为:

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

该按钮调用的JavaScript是:

the JavaScript which that button calls is:

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();
    }
}

屏幕截图:

推荐答案

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

It has to be clicked twice because you are defining #microsubmit's click event inside the function. So the first time you click you bind the event handler, and the 2nd time the event handler is in place and gets fired. I haven't gone over the logic behind what you're trying to accomplish but my guess is that if you move the event binder outside the function and make sure all your variables are in the right scopes then it'll work.

这篇关于需要单击两次按钮才能触发功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆