jQuery 四四方方的问题

发布于 2024-10-07 09:53:25 字数 1078 浏览 0 评论 0原文

我使用 jquery boxy 来调用我的验证码覆盖:

<script>
$(document).ready(function()    {
    var zahl1 = Math.floor(Math.random()*11);
    var zahl2 = Math.floor(Math.random()*31);

    //$("#cap").html("Wie viel ist "+zahl1+" + "+zahl2+"? <input type='text' id='answer' />");

    $("#shortbutton").live("click", function()  {
        ask();
        return false;
    });
});

function ask()
{
    zahl1 = Math.floor(Math.random()*11);
    zahl2 = Math.floor(Math.random()*31);
    var sum = zahl1 + zahl2;
    new Boxy.ask("Wie viel ist "+zahl1+" + "+zahl2+"? <input type='text' id='answ' />", 
        ["Ok"], 

        function(val) {

            if($("#answ").val() == (zahl1 + zahl2))
            {
                $("#form").submit();
            }
            else
            {
                ask();
                return false;
            }
        },

        {title: "Captcha"}
    );
}
</script>

代码有点不干净...函数称它们为 selfts...我知道...但是我怎样才能在 Enter Press 上调用“OK”事件...

所以你写下答案并按 Enter 键...然后它就会检查。

有什么想法吗?

i using a jquery boxy to call my captche overlay:

<script>
$(document).ready(function()    {
    var zahl1 = Math.floor(Math.random()*11);
    var zahl2 = Math.floor(Math.random()*31);

    //$("#cap").html("Wie viel ist "+zahl1+" + "+zahl2+"? <input type='text' id='answer' />");

    $("#shortbutton").live("click", function()  {
        ask();
        return false;
    });
});

function ask()
{
    zahl1 = Math.floor(Math.random()*11);
    zahl2 = Math.floor(Math.random()*31);
    var sum = zahl1 + zahl2;
    new Boxy.ask("Wie viel ist "+zahl1+" + "+zahl2+"? <input type='text' id='answ' />", 
        ["Ok"], 

        function(val) {

            if($("#answ").val() == (zahl1 + zahl2))
            {
                $("#form").submit();
            }
            else
            {
                ask();
                return false;
            }
        },

        {title: "Captcha"}
    );
}
</script>

The code is a bit unclean... function call them selfts... i know... but how can i let call the "OK" event on Enter Press...

So you write the answer and press Enter... and it checks.

Have some an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懵少女 2024-10-14 09:53:25

您可以通过编程方式单击框上的“确定”按钮:

$("#answ").live("keydown", function(event) {
    if (event.which === 13) {
        $(this)
            .parent()
            .siblings("form")
            .children("input:button").click();
    }
});

无论何时创建输入,都使用 live 监听事件。如果您有多个输入类型按钮(例如,如果添加“取消”按钮),则可能需要在检查回车键时修改选择器。

在这里查看一个工作示例: http://jsfiddle.net/andrewwhitaker/Z2qeh/

注意:我尝试将您的匿名回调方法重构为自己的方法,该方法可以由 Boxy 对象和 keydown 侦听器调用,但我无法获得与单击“确定”按钮。

You could programmatically click the 'Ok' button on the box:

$("#answ").live("keydown", function(event) {
    if (event.which === 13) {
        $(this)
            .parent()
            .siblings("form")
            .children("input:button").click();
    }
});

Using live to listen to events no matter when the input is created. You may have to modify selector under the check for the enter key if you have more than one input of type button (if you add a "Cancel" button for example).

Check out a working example here: http://jsfiddle.net/andrewwhitaker/Z2qeh/

Note: I tried refactoring your anonymous callback method into its own method that could be called by both the Boxy object and the keydown listener, but I couldn't get the interaction quite the same as clicking the "Ok" button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文