【问题标题】:send form if all radio button value is true如果所有单选按钮值都为真,则发送表单
【发布时间】:2022-12-08 20:50:49
【问题描述】:

如果所有收音机都是真的,我需要发送表格(有点像是/否 btns),或者如果至少有一个是假的,我需要发送错误。 可以有任意数量的这样的“是/否按钮”。 仅当一切为“是”时才需要发送,如果至少未选择 1 或“否”,则执行其他操作 我试试这个


  <form id="someForm">
  <p>1-st group</p>
  <div class="radio-box">
    <div class="radio-success">
      <input class="form-check-input" type="radio" name="radio1" value="conf">
    </div>
    <div class="radio-error">
      <input class="form-check-input" type="radio" name="radio1" value="err" >
    </div>
  </div>
  <p>2-nd group</p>
  <div class="radio-box">
    <div class="radio-success">
      <input class="form-check-input"  type="radio" name="radio2" value="conf">
    </div>
    <div class="radio-error">
      <input class="form-check-input"  type="radio" name="radio2" value="err">
    </div>
  </div>
  <button id="test">go!</button>
</form>
$('#test').on('click', function(e) {   
    e.preventDefault();


    $('#someForm')
    .find('.radio-box input')
    .each(function () {

    inputs = $('input[type="radio"]');

    if(inputs.is(':checked') && inputs.val() === 'conf')
    {
        console.log('form send');
    }
    else{
        console.log('some is not checked');
    }

    });

});

希望对你有所帮助)谢谢!

【问题讨论】:

    标签: jquery


    【解决方案1】:

    检查已检查并保持值为“err”的无线电类型的输入。如果未选中,find() 的大小将为 0。如果它们与所有单选选项匹配,还要检查是的答案。

    $('#test').on('click', function(e) {
      e.preventDefault();
    
      const radio_no_answers = $('#someForm').find('.radio-box input[type="radio"][value="err"]:checked');
      const radio_yes_answers = $('#someForm').find('.radio-box input[type="radio"][value="conf"]:checked');
    
      if (radio_no_answers.length == 0 && radio_yes_answers.length == 2) {
        console.log('submit');
      } else {
        console.log('err');
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <form id="someForm">
      <p>1-st group</p>
      <div class="radio-box">
        <div class="radio-success">
          <input class="form-check-input" type="radio" name="radio1" value="conf">Yes
        </div>
        <div class="radio-error">
          <input class="form-check-input" type="radio" name="radio1" value="err">No
        </div>
      </div>
      <p>2-nd group</p>
      <div class="radio-box">
        <div class="radio-success">
          <input class="form-check-input" type="radio" name="radio2" value="conf">Yes
        </div>
        <div class="radio-error">
          <input class="form-check-input" type="radio" name="radio2" value="err">No
        </div>
      </div>
      <button id="test">go!</button>
    </form>

    【讨论】:

    • 非常感谢!
    【解决方案2】:
    // Get all radio buttons on the page
    var radios = document.querySelectorAll('input[type="radio"]');
    
    // Loop through each radio button and check if it is selected
    var allSelected = true;
    for (var i = 0; i < radios.length; i++) {
        if (!radios[i].checked) {
            allSelected = false;
            break;
        }
    }
    
    // If all radio buttons are selected, submit the form
    if (allSelected) {
        // Submit the form here
    } else {
        // Handle the error here
    }
    

    此代码首先使用 querySelectorAll 获取页面上的所有单选按钮。然后循环遍历每个单选按钮,检查它是否被选中。如果选择了所有单选按钮,它会提交表单。否则,它会处理错误。

    您可以修改此代码以满足您的特定需求。例如,您可以添加逻辑以仅在每个单选按钮的选定选项为“是”时才提交表单。如果至少有一个单选按钮未被选中或值为“否”,您还可以添加逻辑以向用户显示错误消息。

    【讨论】:

      【解决方案3】:
      if ( $('#someForm').find ('input[type=radio]:not(:checked)').length > 0 )
      {
          // handle error
          
      }
      else
      {
           $('#someForm').submit();
      }
      

      【讨论】:

        猜你喜欢
        • 2016-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-12
        • 1970-01-01
        • 1970-01-01
        • 2019-11-24
        • 1970-01-01
        相关资源
        最近更新 更多