Jquery输入验证依赖于复选框值

Jquery输入验证依赖于复选框值,jquery,html,checkbox,Jquery,Html,Checkbox,我试图避免验证混乱,并使用内联验证。我有一个复选框,后面跟着三个输入框。如果选中该复选框,则输入字段必须变为“必需”。不然就不行了 代码如下: 我有苹果 苹果的状况? 来源于? 量 好的,这可以按如下方式完成。默认情况下,将复选框设为未选中,并在其上写入以下内容jquery $(document).ready(function(){ var atLeastOneIsChecked = $('input[name="vi"]:checked').length > 0;

我试图避免验证混乱,并使用内联验证。我有一个复选框,后面跟着三个输入框。如果选中该复选框,则输入字段必须变为“必需”。不然就不行了

代码如下:


我有苹果
苹果的状况?
来源于?
量

好的,这可以按如下方式完成。默认情况下,将复选框设为未选中,并在其上写入以下内容
jquery

    $(document).ready(function(){
      var atLeastOneIsChecked = $('input[name="vi"]:checked').length > 0;
       if(atLeastOneIsChecked){
        $("input").attr("required", "true");
      }
    else {
        // else do what you need to do with this case
    }
});

给除checkbox div之外的其他div上课,说“check_1”

html:


还有32个其他输入框未链接到此复选框。因此,如果(atlestoneischecked){$(“input”).attr(“required”,“true”);}不起作用,那么只需对问题进行更改即可。我会尽力帮助你的!我需要在提交表单时验证文本框。上述解决方案与此有何关联?
 <div class="col-xs-12">
    <div class="form-group">
        <label class="checkbox-inline">
            <input type="checkbox" id="vi" name="vi" />
            I have Apple
        </label>
    </div>
</div>

<div class="col-xs-6 check_1">
    <div class="form-group">
        <label>Apple condition?</label> 
        <input type="input" class="form-control  input_val" id="appleCond" validate="required"/>
    </div>
</div>

<div class="col-xs-6 check_1">
    <div class="form-group">
        <label>Sourced from ?</label> 
        <input type="input" class="form-control input_val" id="appleSource" validate="required"/>
    </div>
</div>

<div class="col-xs-6 check_1">
    <div class="form-group">
        <label>Quantity</label> 
        <input type="input" class="form-control  input_val" id="appleQuantity" validate="required"/>
    </div>
</div>

<div>
    <input type="button" value="submit" id="button_submit"/>
</div>
var check_box=0;
$(".check_1").hide();


 $('#button_submit').click(function() {
    if(check_box==true)
    {

        $(".input_val").prop('required',true);



    }
     else
     {
        // code when checkbox not selected
     }

});


$('input[name="vi"]').click(function() {

        if ($(this).is(':checked')) {
           $(".check_1").show();
            check_box=true;
        }
    else{
             $(".check_1").hide();
            check_box=false;
    }
});