简体   繁体   English

我如何验证复选框?

[英]how can i validate the checkbox?

 <form target="_self" id="immunization_info_form" class="form-validation save_immune25 update_immune25" role="form" method="POST" enctype="multipart/form-data"> <div class="form-group row" style="margin-top:10px;height:50px;"> <div class="checkbox checkbox-styled col-md-offset-1 col-md-4"> <label style="font-size:15px;"><input type="checkbox" id="checkbox25" name="ch" class="checkbx" value="25"> <span>Hepatitis A vaccine</span></label> </div> <div class="form-group col-md-4"> <!-- Date input --> <input class="form-control edit25" id="date25" name="date" placeholder="Enter Date" value="<?php echo $date[25]; ?>" type="text" required> </div> </div> <div class="row" style="padding:15px;"> <div class="col-md-3 col-md-offset-1"> <div class="form-group"> <h3 style="color:orange;">Clinic Name</h3><br> <input name="clinic_name" id="clinic" class="form-control edit25" type="text" value="<?php echo $clinic_name[25]; ?>" required> <label for="clinic_name"></label> </div> </div> <div class="col-md-4"> <div class="form-group"> <h3 style="color:orange;">Name of the Health practitioner</h3><br> <input name="hp_name" id="hp" class="form-control edit25" type="text" value="<?php echo $practitioner[25]; ?>" required> <label for="hp_name"></label> </div> </div> <div class="col-md-3"> <div class="form-group"> <h3 style="color:orange;">Lot no. of Vaccine</h3><br> <input name="lotno" id="lot" class="form-control edit25" type="text" value="<?php echo $lotno[25]; ?>" required> <label for="lotno"></label> </div> </div> <div class="row col-md-offset-1"> <div class="col-md-6 text-right"> <input type="button" name="submit" value="SAVE" class="save btn btn-lg btn-primary ink-reaction justify" id="save_immune25"> </div> </div> </div> </form> 

i have added my html code also.. 我已经添加了我的HTML代码..

 $('.save').on('click', function() { var chk = $(this).parent().parent().parent().parent().parent().find('input [name="ch"]').attr('class'); if ($("." + chk).attr('checked', false)) { alert("please check the checkbox"); } else { alert("you have checked the checkbox"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

i have tried with this code and getting the alert "please check the checkbox" for both conditions if and else. 我已尝试使用此代码并获取警报“请检查复选框”以了解两种情况,如果还有其他情况。 i just want to validate the checkbox whether it is checked or not .. if checked means it should display the relevant message if not checked also should display the message. 我只想验证复选框是否已选中..如果选中则表示如果未选中则应显示相关信息也应显示该信息。

There are two things i am noticing: 我注意到有两件事:

  1. Instead use .closest() against .parent() multiple times. 而是.closest()使用.closest().parent()
  2. Do not set the attribute in the if condition, instead of .attr() use .prop() . 不要在if条件中设置属性,而不是.attr()使用.prop()

You can change to this 你可以改变这个

var chk = $(this).closest('form').find('input[name="ch"]');// use form if you have one.
if (!$(chk).prop('checked')) {

 $('.save').on('click', function() { var chk = $(this).closest('form').find('input[name="ch"]'); if (!$(chk).prop('checked')) { alert("please check the checkbox"); } else { alert("you have checked the checkbox"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form target="_self" id="immunization_info_form" class="form-validation save_immune25 update_immune25" role="form" method="POST" enctype="multipart/form-data"> <div class="form-group row" style="margin-top:10px;height:50px;"> <div class="checkbox checkbox-styled col-md-offset-1 col-md-4"> <label style="font-size:15px;"><input type="checkbox" id="checkbox25" name="ch" class="checkbx" value="25"> <span>Hepatitis A vaccine</span></label> </div> <div class="form-group col-md-4"> <!-- Date input --> <input class="form-control edit25" id="date25" name="date" placeholder="Enter Date" value="<?php echo $date[25]; ?>" type="text" required> </div> </div> <div class="row" style="padding:15px;"> <div class="col-md-3 col-md-offset-1"> <div class="form-group"> <h3 style="color:orange;">Clinic Name</h3><br> <input name="clinic_name" id="clinic" class="form-control edit25" type="text" value="<?php echo $clinic_name[25]; ?>" required> <label for="clinic_name"></label> </div> </div> <div class="col-md-4"> <div class="form-group"> <h3 style="color:orange;">Name of the Health practitioner</h3><br> <input name="hp_name" id="hp" class="form-control edit25" type="text" value="<?php echo $practitioner[25]; ?>" required> <label for="hp_name"></label> </div> </div> <div class="col-md-3"> <div class="form-group"> <h3 style="color:orange;">Lot no. of Vaccine</h3><br> <input name="lotno" id="lot" class="form-control edit25" type="text" value="<?php echo $lotno[25]; ?>" required> <label for="lotno"></label> </div> </div> <div class="row col-md-offset-1"> <div class="col-md-6 text-right"> <input type="button" name="submit" value="SAVE" class="save btn btn-lg btn-primary ink-reaction justify" id="save_immune25"> </div> </div> </div> </form> 

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

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