【问题标题】:Show Next fieldset OnKeyPress Enter - Multi Step Form显示下一个字段集 OnKeyPress Enter - 多步表单
【发布时间】:2018-06-30 20:25:39
【问题描述】:

我正在使用 jQuery 和 CSS3 处理带有进度条的多步表单 ,来自这个codepen示例 - codepen.io/orozcokapo/pen/LmgjYg

OnClick 鼠标事件工作得非常好。 但它不适用于按回车键。

谁能告诉我,

如何让它显示下一个字段集 OnKeyPress Enter 按钮?

【问题讨论】:

  • 添加另一个事件onkeypress 并通过使用e.keycode 对其进行测试来检查密钥,输入按钮为13
  • 感谢您的回复。它不工作。

标签: javascript jquery fieldset onkeypress


【解决方案1】:

您必须绑定回车按钮才能使其工作

//jQuery time
var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches
var step=1;

$(".next").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    next_fs = $(this).parent().next();

    //activate next step on progressbar using the index of next_fs
    $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

    //show the next fieldset
    next_fs.show(); 
    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale current_fs down to 80%
            scale = 1 - (1 - now) * 0.2;
            //2. bring next_fs from the right(50%)
            left = (now * 50)+"%";
            //3. increase opacity of next_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({
        'transform': 'scale('+scale+')',
        'position': 'absolute'
      });
            next_fs.css({'left': left, 'opacity': opacity});
        }, 
        duration: 800, 
        complete: function(){
            current_fs.hide();
            animating = false;
        }, 
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
  step++;
});

$(".previous").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    previous_fs = $(this).parent().prev();

    //de-activate current step on progressbar
    $("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");

    //show the previous fieldset
    previous_fs.show(); 
    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale previous_fs from 80% to 100%
            scale = 0.8 + (1 - now) * 0.2;
            //2. take current_fs to the right(50%) - from 0%
            left = ((1-now) * 50)+"%";
            //3. increase opacity of previous_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({'left': left});
            previous_fs.css({'transform': 'scale('+scale+')', 'opacity': opacity});
        }, 
        duration: 800, 
        complete: function(){
            current_fs.hide();
            animating = false;
        }, 
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
  step--;
});

$(".submit").click(function(){
    return false;
})
$('input').keypress(function(e) {
    if(e.which == 13) {
      if (step == 1) {
        $('.next.action-button').click();
      } else if (step == 2) {
        $('.next.action-button').last().click();
      } else {
        $('.submit.action-button').click();
      }
    }
});

【讨论】:

  • 感谢您的回答。我已经尝试了给定的代码,但它不起作用。
  • 您是否尝试过在输入字段中对焦后再按回车键?
  • 是的尝试不工作。能否请您发布上述代码的确切方法。
  • 我在输入字段中添加了输入键的监听器。当您关注该字段时,如果您按下回车键,它将触发点击下一步按钮和提交按钮
  • 感谢您的帮助..上述代码在 HTML 中进行了一些调整后工作。 <input type="button" name="next" class="next action-button" value="Next" /> 在这一行中,我只是将输入类型更改为 submit 并且它起到了神奇的作用 - <input type="submit" name="next" class="next action-button" value="Next" />
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
  • 1970-01-01
  • 2019-02-03
  • 1970-01-01
  • 2022-12-14
  • 2023-03-06
相关资源
最近更新 更多