除了鼠标单击之外,还可以使用 Tab 键导航滑动表单

发布于 2024-12-10 04:53:22 字数 2421 浏览 1 评论 0原文

我很头疼!我已经研究这个代码几个小时了,但我不知道为什么它不起作用。我有一个分为多个部分的表单,当一个部分完成后,用户可以单击导航栏中的下一个选项卡以前进到表单的下一部分。下一部分滑入,用户可以继续填写表单。当使用鼠标单击作为功能的操作时,这工作得很好,但是,如果用户使用“Tab”键进行导航,则滑动表单不会正确且完全地滑入。我仍然可以看到表单前一部分的一部分以及表单的当前部分。这是我到目前为止适用于鼠标单击的代码:

$('#navigation a').bind('click',function(e){
    var $this   = $(this);
    var prev    = current;
    $this.closest('ul').find('li').removeClass('selected');
    $this.parent().addClass('selected');
    /*
    we store the position of the link
    in the current variable 
    */
    current = $this.parent().index() + 1;
    /*
    animate / slide to the next or to the corresponding
    fieldset. The order of the links in the navigation
    is the order of the fieldsets.
    Also, after sliding, we trigger the focus on the first 
    input element of the new fieldset
    If we clicked on the last link (confirmation), then we validate
    all the fieldsets, otherwise we validate the previous one
    before the form slided
    */
    $('#steps').stop().animate({
        marginLeft: '-' + widths[current-1] + 'px'
    },500,function(){
        if(current == fieldsetCount)
            validateSteps();
        else
            validateStep(prev);
        $('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();    
    });
    e.preventDefault();
});

现在,当我尝试使其适用于“Tab”键时,我使用此代码(和/或此代码的变体),但似乎没有任何效果

$('#navigation a').bind('keypress',function(e){
    var $this   = $(this);
    var prev    = current;
    $this.closest('ul').find('li').removeClass('selected');
    $this.parent().addClass('selected');
    /*
    we store the position of the link
    in the current variable 
    */
    current = $this.parent().index() + 1;

    var $fieldset = $(this);
    $fieldset.children(':last').find(':input').keypress(function(e){
        if (e.keyCode == 9){
            $('#steps').stop().animate({
        marginLeft: '-' + widths[current-1] + 'px'
    },500,function(){
        if(current == fieldsetCount)
            validateSteps();
        else
            validateStep(prev);
        $('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();    
    });
            e.preventDefault();
        }
    });
});

:可以在 JSFiddle 上查看它的工作原理和不工作原理: http://jsfiddle.net/GTmwU/13/

任何帮助非常感谢!

I am getting a headache! I've been working on this code for hours now, and I have no idea why it's not working. I have a form that is broken up into parts, and when one part is complete, the user can click on the next tab in the navigation bar to advance to the next part of the form. The next part slides in and the user can continue with the form. This works just fine when using the mouse click as the action of the function, however, if the user is using the "Tab" key to navigate, the sliding form does not slide in correctly and completely. I'm left with a portion of the previous part of the form still in view, along with the current part of the form. This is the code I have so far that works for the mouse click:

$('#navigation a').bind('click',function(e){
    var $this   = $(this);
    var prev    = current;
    $this.closest('ul').find('li').removeClass('selected');
    $this.parent().addClass('selected');
    /*
    we store the position of the link
    in the current variable 
    */
    current = $this.parent().index() + 1;
    /*
    animate / slide to the next or to the corresponding
    fieldset. The order of the links in the navigation
    is the order of the fieldsets.
    Also, after sliding, we trigger the focus on the first 
    input element of the new fieldset
    If we clicked on the last link (confirmation), then we validate
    all the fieldsets, otherwise we validate the previous one
    before the form slided
    */
    $('#steps').stop().animate({
        marginLeft: '-' + widths[current-1] + 'px'
    },500,function(){
        if(current == fieldsetCount)
            validateSteps();
        else
            validateStep(prev);
        $('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();    
    });
    e.preventDefault();
});

Now when I try to make it work for the "Tab" key I use this code (and\or variations of this code), but nothing seems to work:

$('#navigation a').bind('keypress',function(e){
    var $this   = $(this);
    var prev    = current;
    $this.closest('ul').find('li').removeClass('selected');
    $this.parent().addClass('selected');
    /*
    we store the position of the link
    in the current variable 
    */
    current = $this.parent().index() + 1;

    var $fieldset = $(this);
    $fieldset.children(':last').find(':input').keypress(function(e){
        if (e.keyCode == 9){
            $('#steps').stop().animate({
        marginLeft: '-' + widths[current-1] + 'px'
    },500,function(){
        if(current == fieldsetCount)
            validateSteps();
        else
            validateStep(prev);
        $('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();    
    });
            e.preventDefault();
        }
    });
});

You can see how it's working and not working over at JSFiddle: http://jsfiddle.net/GTmwU/13/

Any help is greatly appreciated!

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

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

发布评论

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

评论(1

轻拂→两袖风尘 2024-12-17 04:53:22

http://jsfiddle.net/2Fbrt/

这里有几件事。我已经更改了您将事件绑定到的内容。由于某种原因,您已将其添加到导航中,因此它从未触发。我对此进行了一些更改,以便它绑定到每个字段集的最后一个输入(仅 - 不选择)。我还使用了 keydown 而不是 keypress

请注意,您已经完全禁用了 Shift Tab 键,我建议添加另一个绑定来确定您要进入的方向。事实上,有一种更简单的方法可以实现此目的,该方法可以绑定到更多位置,但在其他方面的侵入性较小。我不在这里推荐这个,因为我不知道你的表格的范围。如果您有兴趣,请告诉我!

希望这对您有所帮助,如果我犯了任何错误或留下不清楚的内容,请发表评论。

http://jsfiddle.net/2Fbrt/

Couple of things here. I've change what you are binding the event to. For some reason you had added it to the navigation, so it never fired. I've changed that a bit so it is bound to the last input (only - not select) of each fieldset. I've also used keydown instead of keypress.

Be aware that you have disabled shift tabbing entirely, I would recommend adding in another binding to figure out which direction you are going in. In fact, there is a simpler way of achieving this which binds to more places but is otherwise less intrusive. I'm not recommending this here as I don't know the scope of your form. Let me know if you are interested!

Hope this helps and just drop a comment if I've made any errors or left something unclear.

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