试图验证我的单选按钮。有人可以帮助我吗?

时间:2019-12-18 08:22:07

标签: javascript html

我几天来一直在为自己的问题苦苦挣扎,无法找到解决方案。

我正在尝试验证我的单选按钮,当我提交表单时,我希望它验证单选按钮是否为空,然后从引导程序中添加一个is-invalid类,并且是否选中了某些按钮,我希望它添加类有效。我现在提交表单时,只会添加is-invalid类。

HTML

<fieldset class="form-group col-md-6">
    <div class="col">
        <legend class="col-form-label col-md-6">NewsFrek </legend>
        <div class="col-md-5">
            <div class="form-check">
                <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios1" value="option1">
                <label class="form-check-label" for="gridRadios1">
                    Every Week
                </label>
            </div>
            <div class="form-check col">
                <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios2" value="option2">
                <label class="form-check-label" for="gridRadios2">
                    Every month
                </label>
            </div>
            <div class="form-check">
                <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3">
                <label class="form-check-label" for="gridRadios3">
                    Every year
                </label>
            </div>
        </div>
    </div>
</fieldset>

JavaScript

if(checkCheckBoxen.checked == true){
    $(checkCheckBoxen).addClass('is-valid')   
} else{
    $(checkCheckBoxen).removeClass('is-valid')
    $(checkCheckBoxen).addClass('is-invalid')
}

2 个答案:

答案 0 :(得分:0)

我实际上不知道这是否可以解决您的问题,因为我不确定是否理解。

HTML:

<fieldset>   
<div class="col">
                <legend class="col-form-label col-md-6">NewsFrek </legend>
                <div class="col-md-5">
                  <div class="form-check">
                    <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios1" value="option1">
                    <label class="form-check-label" for="gridRadios1">
                      Every Week
                    </label>
                  </div>
                  <div class="form-check col">
                    <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios2" value="option2">
                    <label class="form-check-label" for="gridRadios2">
                      Every month
                    </label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3">
                    <label class="form-check-label" for="gridRadios3">
                      Every year
                    </label>
                  </div>
                </div>
              </div>
            </fieldset>
<a href='javascript:;' class="btn btn-primary ml-5 mt-2" onClick='test()'>Submit Me</>

Javascript:

test = () => {
  $(".form-check-input").each(function(index, element) {
        var $this = $(this);
        if (this.checked) {
          $(element).removeClass('is-invalid')
          $(element).addClass('is-valid');
        }else{
          $(element).addClass('is-invalid')
        }
   });
}

这里是a codepen link

答案 1 :(得分:0)

m2m_changed
if tblLog.Locate('USERDATE', '2015-10-26', []) then ...

在提交表单之前进行表单验证,您可以根据需要进行修改