【问题标题】:jQuery change FIELDSET depending on RADIO BUTTONjQuery 根据 RADIO BUTTON 更改 FIELDSET
【发布时间】:2018-10-14 10:59:06
【问题描述】:

我正在尝试实现以下代码。 codepen.io 但我想要的不同是,每当用户点击红色时,它都会跳过下一步并跳转到另一个步骤。 例如,如果在第 1 步单击红色,那么它将跳过第 2 步并继续执行第 3 步。 在 javascript 代码中,如果 red 的值大于 1,则添加条件

next_fs.hide();
next_fs.show();

据我所知,它应该可以工作。在这一点上,我不知道我应该做什么。

【问题讨论】:

  • 总是跳到3步吗?如果不是,你去哪里时需要任何映射。
  • 根据选择的选项在任何我想要的地方进行操作。例如,我提到了第 3 步

标签: javascript jquery html css forms


【解决方案1】:

请尝试以下代码。

 if($(this).parent().find('.control-checkbox input:checked').val() == "red"){
        next_fs = $(this).parent().next().next();
    }else{
      next_fs = $(this).parent().next();
  }

在下次点击功能中添加以上代码。

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

    current_fs = $(this).parent();
  //console.log($(this).parent().find('.control-checkbox input:checked').val());
  if($(this).parent().find('.control-checkbox input:checked').val() == "red"){
        next_fs = $(this).parent().next().next();
    }else{
      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'
    });
});

上一次点击。请更新以下代码。

 if($(this).parent().prev().prev().find('.control-checkbox input:checked').val() == "red"){
        previous_fs = $(this).parent().prev().prev();
    }else{
      previous_fs = $(this).parent().prev();
  }

在之前的点击函数中添加以上代码。

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

    current_fs = $(this).parent();

  //console.log($(this).parent().prev().prev().find('.control-checkbox input:checked').val());
  if($(this).parent().prev().prev().find('.control-checkbox input:checked').val() == "red"){
        previous_fs = $(this).parent().prev().prev();
    }else{
      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'
    });
});

【讨论】:

  • 但这里的问题是,如果我点击返回,它会转到第 2 步,它应该返回第 1 步
  • @taufeeqzaman 看到我的回答,完全按照您的意愿行事,而且是动态的
  • @taufeeqzaman 我已经更新了之前的点击功能。请检查上面的代码。
【解决方案2】:

我会在输入中添加一个数据属性,例如data-skip="2"。基本上说“如果选择了这个,跳过2”。然后,代替$(this).parent().next(),您检查当前步骤中的选定值并使用$(this).parent().nextAll().slice(0, skipSteps).last() 选择下一个视图。

所以例如你会得到这个:

current_fs = $(this).parent();
next_step = current_fs.find('input:checked').attr('data-skip') || 1;
next_fs = $(this).parent().nextAll().slice(0, parseInt(next_step, 10)).last();

检查我的叉子:https://codepen.io/tbleckert/pen/aRLLwE?editors=1010

【讨论】:

    猜你喜欢
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多