简体   繁体   English

如何确保在表单内选择了所有文本字段和单选按钮?

[英]How do i make sure ALL textfields & Radio Buttons are selected inside a form?

I have a form with textfield, textareas and radio buttons.我有一个带有文本字段、文本区域和单选按钮的表单。 I want to make sure everything inside the div is selected, therefore i have disabled the submit button until everything is selected.我想确保选择了 div 中的所有内容,因此我禁用了提交按钮,直到选择了所有内容。 But i can't seem to get it to work.但我似乎无法让它工作。 I have a jquery functions which works, when i check on all the required input fields, but it doesn't work on radio buttons.我有一个 jquery 函数,当我检查所有必需的输入字段时,它可以工作,但它在单选按钮上不起作用。 Here is what is working:这是有效的:

$(document).ready(function(){
  function checkInputs() {
    var validInputs = true;
    $('input').filter('[required]').each(function() {
      if ($(this).val() === '') {
        validInputs = false;
        return false;
      }
    });

    if(validInputs) {$('#speichern').prop('disabled', false)}
    return validInputs;
  }

  //Enable or disable button based on if inputs are filled or not
  $('input').filter('[required]').on('keyup',function() {
    checkInputs();
  })

  checkInputs();

});

How can i include the radio buttons inside this function?如何在此功能中包含单选按钮?

Try this inside checkInputs()checkInputs()试试这个

$('input').filter('[required]').each(function() {
  var elm = this,
    $elm = $(elm);
  switch (elm.type) {
    case 'text':
      if ($elm.val() === '') {
        validInputs = false;
        return false;
      }
      break;
    case 'radio':
      if (!elm.checked) {
        validInputs = false;
        return false;
      }
      break;
    case 'checkbox':
      // handle checkbox
      break;
    default:
      // handle others
      break;
  }
});

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

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