【问题标题】:Radio button validation in multi step form多步骤表单中的单选按钮验证
【发布时间】:2019-08-30 09:39:40
【问题描述】:

我正在尝试在我的多步骤表单中使用验证,当我使用验证时,它在最后一步中的输入字段可以正常工作,但问题是,在第一步和第二步中,我正在使用单选选项并且我正在尝试在那里使用验证,以便在不选择任何选项的情况下无法进入下一步。

$(document).ready(function() {
  jQuery('#sucess').hide();
  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
  $(".next").click(function(event) {

    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 + ')'
        });
        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'
    });





  });

  $(".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'
    });
  });

  $("#msform").submit(function() {

    var location = jQuery('#location').val();
    if ($.trim(location).length == 0) {
      document.getElementById("location").style.borderColor = "#E34234";
      jQuery('.fs-error').html('<span style="color:red;"> Please Enter Your Location !</span>');
      jQuery('.fs-error').show();
      return false;
    }

    var date = jQuery('#date').val();
    if ($.trim(date).length == 0) {
      document.getElementById("date").style.borderColor = "#E34234";
      jQuery('.fs-error').html('<span style="color:red;"> Please Enter the Date !</span>');
      jQuery('.fs-error').show();
      return false;
    }

    var name = jQuery('#name').val();
    if ($.trim(name).length == 0) {
      document.getElementById("name").style.borderColor = "#E34234";
      jQuery('.fs-error').html('<span style="color:red;"> Please Enter Your Name !</span>');
      jQuery('.fs-error').show();
      return false;
    }

    var email = jQuery('#email').val();
    if ($.trim(email).length == 0) {
      document.getElementById("email").style.borderColor = "#E34234";
      jQuery('.fs-error').html('<span style="color:red;"> Please Enter Your Email !</span>');
      jQuery('.fs-error').show();
      return false;
    }

    var phone = jQuery('#phone').val();
    if ($.trim(phone).length == 0) {
      document.getElementById("phone").style.borderColor = "#E34234";
      jQuery('.fs-error').html('<span style="color:red;"> Please Enter Your Phone Number !</span>');
      jQuery('.fs-error').show();
      return false;
    }

  });


});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<form id="msform" method="POST" action="abc.php">
  <!-- progressbar -->
  <ul id="progressbar">
    <li class="active"></li>
    <li></li>
    <li></li>
  </ul>
  <!-- fieldsets -->
  <fieldset>
    <div class="row">
      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="artist" type='radio'>
                                <span class='btn first'>Singers</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="artist" type='radio'>
                                <span class='btn'>Bands</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="artist" type='radio'>
                                <span class='btn'>Rappers</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="artist" type='radio'>
                                <span class='btn'>Djs</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="artist" type='radio'>
                                <span class='btn'>Comedians</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="artist" type='radio'>
                                <span class='btn'>Motivational Speakers</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="artist" type='radio'>
                                <span class='btn'>Dancers</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="artist" type='radio'>
                                <span class='btn'>Actors</span>
                            </label>
        </div>
      </div>
    </div>
    <input type="button" name="next" class="next action-button" value="NEXT" />
  </fieldset>


  <fieldset>
    <h5 class="concierge-mobile desktop-view">Celeb Concierge</h5>
    <h2 class="artist-type">Type of Event</h2>
    <div class="row">
      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="event" type='radio'>
                                <span class='btn first'>Wedding</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="event" type='radio'>
                                <span class='btn'>Private Party</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="event" type='radio'>
                                <span class='btn'>Brand Launch</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="event" type='radio'>
                                <span class='btn'>Corporate Get-Together</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="event" type='radio'>
                                <span class='btn'>Collegue Event</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="event" type='radio'>
                                <span class='btn'>Charity</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="event" type='radio'>
                                <span class='btn'>Photo/Video Shoot</span>
                            </label>
        </div>
      </div>

      <div class="col-md-6 my-2">
        <div class='btns'>
          <label>
                              <input name="event" type='radio'>
                                <span class='btn'>Concert</span>
                            </label>
        </div>
      </div>
    </div>
    <input type="button" name="previous" class="previous action-button" value="BACK" />
    <input type="button" name="next" class="next action-button" value="NEXT" />
  </fieldset>


  <fieldset>
    <h5 class="concierge-mobile desktop-view">Celeb Concierge</h5>
    <h2 class="artist-type">Type of Event</h2>
    <div class="row">
      <div class="col-md-12 my-2">
        <h6 class="budget">Budget</h6>
        <div class="rangeslider">
          <input class="min" name="range_1" type="range" min="1" max="100" value="10" />
          <input class="max" name="range_1" type="range" min="1" max="100" value="90" />
          <span class="range_min light left">10.000</span>
          <span class="range_max light right">90.000</span>
        </div>
      </div>
      <div class="col-md-6 my-2">
        <input type="text" name="location" id="location" class="event-details" placeholder="Event Location" />
      </div>
      <div class="col-md-6 my-2">
        <input type="text" name="date" id="date" class="event-details" placeholder="Event Date" />
      </div>
      <div class="col-md-6 my-2">
        <input type="text" name="name" id="name" class="event-details" placeholder="Name" />
      </div>
      <div class="col-md-6 my-2">
        <input type="text" name="email" id="email" class="event-details" placeholder="Email Id" />
      </div>
      <div class="col-md-6 my-2">
        <input type="text" name="phone" id="phone" class="event-details" placeholder="Phone Number" />
      </div>
    </div>
    <input type="button" name="previous" class="previous action-button" value="BACK" />
    <button class="book-button" type="submit">SUBMIT</button>
  </fieldset>

</form>

提前致谢。

【问题讨论】:

  • 我建议使用 disabled 初始化下一个按钮并在单选按钮上设置事件监听器(.change() 可以工作)。选择单选框后,您可以使用$('mybuttonselector').removeAttr('disabled'); 删除disabled 属性并使其可点击。您可以为每个下一个按钮提供自己的 ID,以便更轻松地选择适用的按钮
  • 谢谢哥们的推荐,真的很有帮助

标签: javascript jquery


【解决方案1】:

这里是您的完整代码。

<html>
	<head>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
	</head>
	<body>
		<form id="msform" method="POST" action="abc.php">
		   <!-- progressbar -->
		   <ul id="progressbar">
			  <li>Step1</li>
			  <li>Step2</li>
			  <li>Step3</li>
		   </ul>
		   
		   <!-- error messages -->
		   <span class="fs-error"></span>
		   
		   <!-- fieldsets -->
		   <fieldset>
			  <div class="row">
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="artist" type='radio'>
					   <span class='btn first'>Singers</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="artist" type='radio'>
					   <span class='btn'>Bands</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="artist" type='radio'>
					   <span class='btn'>Rappers</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="artist" type='radio'>
					   <span class='btn'>Djs</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="artist" type='radio'>
					   <span class='btn'>Comedians</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="artist" type='radio'>
					   <span class='btn'>Motivational Speakers</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="artist" type='radio'>
					   <span class='btn'>Dancers</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="artist" type='radio'>
					   <span class='btn'>Actors</span>
					   </label>
					</div>
				 </div>
			  </div>
			  <input type="button" name="next" class="next action-button" value="NEXT" />
		   </fieldset>
		   <fieldset>
			  <h5 class="concierge-mobile desktop-view">Celeb Concierge</h5>
			  <h2 class="artist-type">Type of Event</h2>
			  <div class="row">
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="event" type='radio'>
					   <span class='btn first'>Wedding</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="event" type='radio'>
					   <span class='btn'>Private Party</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="event" type='radio'>
					   <span class='btn'>Brand Launch</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="event" type='radio'>
					   <span class='btn'>Corporate Get-Together</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="event" type='radio'>
					   <span class='btn'>Collegue Event</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="event" type='radio'>
					   <span class='btn'>Charity</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="event" type='radio'>
					   <span class='btn'>Photo/Video Shoot</span>
					   </label>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<div class='btns'>
					   <label>
					   <input name="event" type='radio'>
					   <span class='btn'>Concert</span>
					   </label>
					</div>
				 </div>
			  </div>
			  <input type="button" name="previous" class="previous action-button" value="BACK" />
			  <input type="button" name="next" class="next action-button" value="NEXT" />
		   </fieldset>
		   <fieldset>
			  <h5 class="concierge-mobile desktop-view">Celeb Concierge</h5>
			  <h2 class="artist-type">Type of Event</h2>
			  <div class="row">
				 <div class="col-md-12 my-2">
					<h6 class="budget">Budget</h6>
					<div class="rangeslider">
					   <input class="min" name="range_1" type="range" min="1" max="100" value="10" />
					   <input class="max" name="range_1" type="range" min="1" max="100" value="90" />
					   <span class="range_min light left">10.000</span>
					   <span class="range_max light right">90.000</span>
					</div>
				 </div>
				 <div class="col-md-6 my-2">
					<input type="text" name="location" id="location" class="event-details" placeholder="Event Location" />
				 </div>
				 <div class="col-md-6 my-2">
					<input type="text" name="date" id="date" class="event-details" placeholder="Event Date" />
				 </div>
				 <div class="col-md-6 my-2">
					<input type="text" name="name" id="name" class="event-details" placeholder="Name" />
				 </div>
				 <div class="col-md-6 my-2">
					<input type="text" name="email" id="email" class="event-details" placeholder="Email Id" />
				 </div>
				 <div class="col-md-6 my-2">
					<input type="text" name="phone" id="phone" class="event-details" placeholder="Phone Number" />
				 </div>
			  </div>
			  <input type="button" name="previous" class="previous action-button" value="BACK" />
			  <button class="next" type="submit">SUBMIT</button>
		   </fieldset>
		</form>
	</body>
	<script>
		$(document).ready(function() {
		  
		  $(".next").click(function(event) {
			var current_index = $(this).parent().index("fieldset");
			
			if(validateStep(current_index)){
				makeStepActive(current_index+1);
			}else{
				event.preventDefault();
			}
		  });

		  $(".previous").click(function() {
			var current_index = $(this).parent().index("fieldset");
			makeStepActive(current_index - 1);
		  });

		 makeStepActive(0);	
		 
		});
	
		function makeStepActive(index){
			var step = $("#progressbar li:eq("+index+")");
			var feildset = $("fieldset:eq("+index+")");
			if(step.length){
				$("#progressbar li").hide();
				$("fieldset").hide();
				step.show();
				feildset.slideDown(500);
			}
		}
	
		function validateStep(step){
			switch(step){
				case 0:
					if(($("input[name='artist']:checked").length === 0)){
						alert('select any artist!');
						return false;
					}
					return true;
				break;
				case 1:
					if(($("input[name='event']:checked").length === 0)){
						alert('select any event!');
						return false;
					}
					return true;
				break;
				default:
					var ids = ['location', 'date', 'name', 'email', 'phone'];
					var err = [];
					ids.forEach(function(id, i){
						var value = $("#"+id).val();
						if ($.trim(value).length === 0) {
						  document.getElementById(id).style.borderColor = "#E34234";
						  err.push('Please Enter Your '+id);
						}
					});
					
					if(err.length > 0){
						$('.fs-error').html('<span style="color:red;">'+err.join('!<br>')+'</span>');
						$('.fs-error').show();
						return false;
					}else{
						return true;
					}
			}
		}
		
	</script>
	
</html>

注意:始终编写最少的代码,以便他人更容易理解。

【讨论】:

  • 但我的做法不同,我禁用了按钮,选择按钮后启用
  • 仅供参考。如果您对我的回答感到满意,可以点赞并接受。
【解决方案2】:

检查是否选择了特定组中的任何单选按钮的简单方法,您可以在验证函数中执行以下操作:

$("#msform").submit(function() {

  //Your other validations here...

if (document.querySelectorAll('input[name="artist"]:checked').length > 0) {
    alert('Artist group has a selection.');
} else {
    alert('Artist group does not have a selection.');
}

if (document.querySelectorAll('input[name="event"]:checked').length > 0) {
    alert('Event group has a selection.');
} else {
    alert('Event group does not have a selection.');
}
}

【讨论】:

  • 哦,抱歉,该函数需要在您的验证函数中调用。让我看看我是否可以进行相应的编辑。
  • 我修改了我的帖子,现在应该修复了。
  • 我不知道为什么会这样,但简单地说,调用这个:document.querySelectorAll('input[name="event"]:checked').length &gt; 0 如果选择了命名组“事件”的任何一个无线电输入,则返回 true,如果没有选择则返回 false其中被选中。这应该是验证是否从特定组中检查了至少 1 个无线电输入所需的全部内容。
猜你喜欢
  • 2021-12-03
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 2014-07-06
  • 2013-02-01
  • 2013-12-04
相关资源
最近更新 更多