首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选中多个复选框时,对其他复选框进行验证

选中多个复选框时,对其他复选框进行验证
EN

Stack Overflow用户
提问于 2015-06-10 10:33:27
回答 1查看 685关注 0票数 0

我有一个有一系列复选框的表单。选中另一个复选框时,如果不填写另一个文本框,则会出现警报。

如果我选择了多个复选框,它根本不验证吗?

代码语言:javascript
复制
<form>
  <div id="form-2" class="pages active" data-page="form-2" style="display: block;">
  <p>Please indicate below the quality issue(s) you were not happy with. Pick as many options as apply. <span class="required">*</span></p>
    <input type="checkbox" name="qual-form-2[]" value="test">
    <input type="checkbox" name="qual-form-2[]" value="test">
    <input type="checkbox" name="qual-form-2[]" value="test">
    <input type="checkbox" name="qual-form-2[]" value="test">
    <input type="checkbox" name="qual-form-2[]" value="test">
    <input type="checkbox" name="qual-form-2[]" value="test">
    <input type="checkbox" name="qual-form-2[]" value="test">
    <input type="checkbox" name="qual-form-2[]" value="test">
    <input type="checkbox" name="qual-form-2[]" value="test">
    <input type="checkbox" name="qual-form-2[]" value="test">
    <input type="checkbox" name="qual-form-2[]" value="test">
    <input type="checkbox" name="qual-form-2[]" value="Other, please specify in the box below">
    <input type="text" name="qual-form-other-form-2" value="" size="40" class="wpcf7-form-control wpcf7-text" aria-invalid="false">
   <button class="next-page" data-page-id="form-2">Next</button>
  </div>
</form>

    <script>
    if (pageCurrent.data('page') == 'form-2') {
      var answer = pageCurrent.find('input[name="qual-form-2[]"]:checked').val();

     if ( (answer == 'Other, please specify in the box below') && ($('input[name="qual-form-other-form-2"]').val().length > 0)) {
       $('input[name="qual-form-other-form-2"]').removeClass('empty');
           return true;
     } else if ( (answer == 'Other, please specify in the box below') && (!$('input[name="qual-form-other-form-2"]').val().length > 0)) {
           alert('Please specify other');
           $('input[name="qual-form-other-form-2"]').addClass('empty');
               return false;
     } else if ( (answer == 'Other, please specify in the box below') && (!$('input[name="qual-form-other-form-2"]').val().length > 0) && ($('input[name="qual-form-2[]').is(":checked"))) {
          alert('Please specify other');
          $('input[name="qual-form-other-form-2"]').addClass('empty');
             return false;
     } else if ($('input[name="qual-form-2[]').is(":checked")) {
             return true;
     } else { 
             alert('Validation errors occurred. Please confirm the fields and submit it again.');
     }

    }
   </script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-10 13:02:03

试一试:更改您的按钮type="button"并调用以下脚本。

代码语言:javascript
复制
$(function(){
    $('.next-page').click(function(){
        var checkCount = $('input[name="qual-form-2[]"]:checked').length;
        //check atleast one checkbox checked
        if(checkCount > 0)
        {
           var checkOther = $('input[name="qual-form-2[]"]:last');
           var checkNotOther = $('input[name="qual-form-2[]"]:checked').not(checkOther);
           //if 'other' and any of the rest is checked show alert
            if(checkNotOther.length > 0 && checkOther.is(':checked'))
           {
               $('input[name="qual-form-other-form-2"]').addClass('empty');
                alert('Please select either "Other" or another checkbox');
           }
           //if other checked and input is empty show alert
            else if(checkOther.is(':checked') && $('input[name="qual-form-other-form-2"]').val() == "")
            {
               $('input[name="qual-form-other-form-2"]').addClass('empty'); 
                alert('Please specify other');
            }
            else
            {
               alert('Validation Succeeded'); 
            }

        }
        else
        {
            alert('Please check atleast one checkbox');
        }
    });
});

JSFiddle演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30753813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档