简体   繁体   English

如何验证表单中的单选按钮字段是否为OR?

[英]How to validate Radio button fields in form Either or OR?

I have a radio form elements with other fields as well on a webpage, there have two types of form radio field, one is vegetable and other is meat. 我有一个无线电表格元素与其他字段以及网页,有两种形式的无线电领域,一个是蔬菜,另一个是肉。 I want to select like these "If they select a meat they have to select a veggie but if they only select a veggie they don't have to select a meat" here is the screenshot of these radio fields http://prntscr.com/nhu6ao 我想选择这些“如果他们选择肉类,他们必须选择蔬菜,但如果他们只选择素食他们不必选择肉”这里是这些无线电领域的截图http://prntscr.com / nhu6ao 在此输入图像描述

jQuery('.radio').change(function() {
  jQuery(this).parent().removeClass('error');
  jQuery(this).parent().parent().removeClass('error');
  if (jQuery(this).hasClass('box-input')) {
    jQuery('#preseason-error').slideUp("slow");
    jQuery('#preseason-title').css("color", "#444");
    jQuery('.boxes-select').removeClass('error');
  }
})

Results will be: If they select a meat they have to select a veggie but if they only select a veggie they don't have to select a meat 结果将是:如果他们选择一种肉,他们必须选择一个素食,但如果他们只选择一个素食他们不必选择一个肉

Voila! 瞧!

I have prepped a simple example for you. 我为你准备了一个简单的例子。 Before we begin I would like to point out 2 things. 在我们开始之前,我想指出两件事。 It is very inefficient to create an event listener for every form element, as you did by calling the .on('.radio') . 为每个表单元素创建一个事件监听器是非常低效的,就像调用.on('.radio') Good practice here is to add event listener on the form ( or any other wrapping element - even body ), and than use the event.target as your reference to the form element that was just clicked by the user. 这里的好习惯是在表单上添加事件监听器(或任何其他包装元素 - 甚至是正文),然后使用event.target作为对用户单击的表单元素的引用。

Another thing is that from the UX perspective I would suggest moving the Meat dishes above the Veggies, as it does not make sense to show it as a first choice if Veggies are somehow dependant on Meats. 另一件事是,从用户体验的角度来看,我建议将肉类菜肴移到蔬菜上方,因为如果蔬菜以某种方式依赖于肉类,将它作为首选来说是没有意义的。

JSfiddle with working example: https://jsfiddle.net/mkbctrlll/7209ayr3/45/ JS提供工作示例: https ://jsfiddle.net/mkbctrlll/7209ayr3/45/

const $meals = $('#meal-options')

$meals.on('change', function(event) {
    if(event.target.name === 'meat') {
    console.log('Meat selected!')
    $('input[name="veggie"]').attr('required', 'true')
  }
})
<form id="meal-options">
  <h2>
    Meat options
  </h2>

  <label for="radio-1">
    <input id="radio-1" name="meat" type="radio">
    Meat 1
  </label>

  <label for="radio-2">
    <input id="radio-2" name="meat" type="radio">
    Meat 2
  </label>

  <h2>
    Veggie options
  </h2>

  <label for="radio-3">
    <input id="radio-3" name="veggie" type="radio">
    veggie 1
  </label>

  <label for="radio-4">
    <input id="radio-4" name="veggie" type="radio">
    veggie 2
  </label>

  <label for="radio-5">
    <input id="radio-5" name="veggie" type="radio">
    veggie 3
  </label>

  <input type="submit">
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM