javascript - 我如何验证复选框?

标签 javascript php jquery html

<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>

我也添加了我的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>

我已经尝试使用此代码并在 if 和 else 两种情况下都收到警告“请选中复选框”。 我只想验证复选框是否被选中.. 如果选中意味着它应该显示相关消息如果没有选中也应该显示消息。

最佳答案

我注意到两件事:

  1. 而是对 .parent() 多次使​​用 .closest()
  2. 不要在 if 条件中设置属性,使用 .prop() 代替 .attr()

你可以改成这个

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>

关于javascript - 我如何验证复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43486671/

相关文章:

javascript - 在 JavaScript 中创建函数的不同方式

php - 正则表达式到 preg_replace '\n' 和字符串中的所有其他转义字符?

php - 导航子菜单项未出现在 iframe 中

javascript - 如何根据自定义属性分别为具有相同类的所有元素设置背景颜色

javascript - 为什么 Twitter Bootstrap 类 navbar-fixed-top 不起作用?

javascript - 如何将使用javascript制作的网站保存到文件中

javascript - 拼接数组以添加到现有数组

javascript - 带有 NodObjC 的 NSStatusBar

php - 如何从自动增量字段获取字段值

javascript - 提高大表上的 iScroll 性能