html - 多步表单 - 在样式单选按钮之间切换

标签 html css forms jquery

我已经构建了一个可以工作的多步骤表单,但它的编码不是很“优雅”, 所以我问 征求您的建议以提高效率, 我在这里只放了 3 步中的 2 步,因为第一步 - 电子邮件名称等'与我的问题无关:

在每个步骤 2 和 3 中都有 2 个样式的单选按钮是和否供用户选择, 在每一步中,我都需要在选中和取消选中样式图像之间切换,当然要防止两者 yes 和 no check 图像将同时显示。

我知道默认/未设置样式的单选按钮行为会阻止同时选中两个按钮 - 我可以在这里使用它来节省一些代码行吗?

html(index.php)

   <form method="post"  id="userForm" action="process_form.php">
<fieldset class="formFieldset">
   <div id="second_step" class="vanish">
      <div class="form  slide_two check_wrap">
         <div class="quizyes quizbtn">
           <img class="uncheck_pic  pic  one"  src="images/check_not.png">
           <img class="check_pic  pic  agree"  src="images/check_bgfull.png" style="display: none;">
           <h1 class="quizText">yes</h1>
        </div>
         <div class="quizno quizbtn">
           <img class="uncheck_pic   pic  two"  src="images/check_not.png">
           <img class="check_pic not  not_agree pic first_not"  src="images/check_bgfull.png" style="display: none;">
           <h1 class="quizText">no</h1>
         </div>
         <div id="feedback_wrap"><div class="feedback"></div></div>
         <div id="submit_wrap" >
            <input type="radio" class="yep decideOne" val ="1" name="yep"   style="display: none;"/>
            <input type="radio" class="nope decideOne" val ="2" name="nope"   style="display: none;"/>
         </div>
    </div></div>

                                          <!-- end of second step -->
    <!--  third step -->
  <div id="third_step" class="vanish">
    <div class="form check_wrap">
        <div class="quizyes quizbtn">
            <img class="uncheck_pic  pic  one"  src="images/check_not.png">
            <img class="check_pic pic  agree"  src="images/check_bgfull.png" style="display: none;">
            <h1 class="quizText">yes</h1>
           </div>
        <div class="quizno quizbtn">
            <img class="uncheck_pic pic two"  src="images/check_not.png">
            <img class="check_pic not not_agree pic second_not"  src="images/check_bgfull.png" style="display: none;">
            <h1 class="quizText">no</h1>
         </div>
        <div id="feedback_wrap"><div class="feedback"></div></div>
            <div id="submit_wrap">
                <input type="radio" class="yep decideTwo" val ="1" name="yep"   style="display: none;"/>
                <input type="radio" class="nope decideTwo" val ="2" name="nope"  style="display: none;"/>
                </div>                                  
        </div></div>
            <!-- end of third step -->

            </fieldset>
                <div id="submit_wrap">
            <input class="submit btn" type="button" name="submit_all" id="submit_all" value="" />
            </div>
                </form>

脚本

$(document).ready(function(){
    $(function() {
    $('.check_pic').hide();
    //original field values
    var isDecide= false;
    //toggle images and set values
$('.pic').on('click', function(event) {
    if ($(this).hasClass('uncheck_pic') && $(this).hasClass('one') ){ 
                     $(".yep").val('agree');
                    $(this).hide();
                    $(this).siblings('.check_pic').show();
                    $(".not").hide();
                    $(".two").show();
            } 
    else if ($(this).hasClass('uncheck_pic') && $(this).hasClass('two') ){ 
                    var isDecide = $(".nope").val('notagree');
                    $(this).hide();
                    $(this).siblings('.check_pic').show();
                    $('.agree').hide();
                     $(".one").show();
            }       
    else if ($(this).hasClass('check_pic') && $(this).hasClass('agree') ){ 
                    $(this).hide();
                    $(this).siblings('.uncheck_pic').show();
            }       
    else if ($(this).hasClass('check_pic') && $(this).hasClass('not_agree') ){ 
                    $(this).hide();
                    $(this).siblings('.uncheck_pic').show();
            }       
});
    // start the submit thing

    $('#submit_all').click(function() {
       if($('#second_step').is(":visible")) {
          $('.decideOne').removeClass('error valid');

       // prevent empty boxes and display a message

       if($('.one').is(":visible") && $('.two').is(":visible")) {
        $('.feedback').text('please select one').show();
        return false;
            }
               // case the user selects yes
       if($('.agree').is(":visible")) {
          $('.feedback').text('thank you for selecting yes').show();
             var isDecide = $(".yep").val();
             var name = $("#firstname").val();
             var phone = $("#phone").val();
             var email = $("#email").val();
              var dataString = 'user-details:name=' + name + ' phone=' + phone + ' email=' + email + ' decide=' +  isDecide ;
            $.ajax({
                type : "POST",
                url : "/",
                data: dataString,
                success : function(data) {
                console.log('data');
                $('#second_step').delay(1000).fadeOut(600, function() {
                $('#first_step').fadeIn(400);
                $('.feedback').hide();
            });
                }
            }); 
               }
                     // case the user selects no
        if($('.first_not').is(":visible")) {
            $(".yep").val();
            $(".nope").val();
            $('#second_step').fadeOut(600, function() {
                $('#third_step').fadeIn(600);
                $('.feedback').hide();
            });
        }
        return false;

// end second step

    } else if($('#third_step').is(":visible")) {

        $('.third_input').removeClass('error').removeClass('valid');
      // prevent empty boxes and display a message 
      if($('.quizyes .one').is(":visible") && $('.quizno .two').is(":visible")) {
        $('.feedback').text('please select one').show();
        return false;
    }
     // if decide yes then submit
      if($('.agree').is(":visible")) {
      $('.feedback').text('thank you for selecting yes').show();
         var isDecide = $(".yep").val();
         var name = $("#firstname").val();
         var phone = $("#phone").val();
         var email = $("#email").val();
          var dataString = 'user-details:name=' + name + ' phone=' + phone + ' email=' + email + ' decide=' +  isDecide ;
      $.ajax({
            type : "POST",
            url : "/",
            data: dataString,
            success : function(data) {
            console.log('data');
            $('#second_step').delay(1000).fadeOut(600, function() {
            $('#first_step').fadeIn(400);
            $('.feedback').hide();
                });
            }
                });//end ajax 
         return true;   
        }//end if agree is visible

        // if decide no then send message and quit
        if($(".second_not").is(":visible")) {
            $(".nope").val("no");
            $('.feedback').text('too bad bye bye').show();
                $('#third_step').fadeOut(3000, function() {
                $('#first_step').fadeIn(600);
                $('.feedback').hide();
            });
        }
                }
        // end third step

        });
        //end submit_all

    }) // general function
  }); // document ready

最佳答案

伙计,我希望你看看优秀的 js 库 - 用于表示所有此类显示、隐藏功能和表单输入、复选框、单选按钮修改的剔除。

关于html - 多步表单 - 在样式单选按钮之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18802130/

相关文章:

html - 添加 "position: fixed;"可防止所有滚动,即使是在未附加的元素上

html - 我的网站没有响应的问题

来自构造函数的 C# 字符串未传递给表单

javascript - 如何使用 jQuery 以编程方式提交表单?

javascript - div 坚持一个位置 :fixed link?

javascript - Bootstrap 模态重叠

javascript - 如何解决跨域请求被阻塞?

html - 如何隐藏输入表单中的提示?

html - Swift 把 Html 代码放到 URL

html - 垂直滑动 CSS 背景图片