javascript - 在迭代中根据类选择子元素

标签 javascript jquery

使用 Vanilla Javascript 或 jQuery。

// html
<input type="checkbox" value="1" checked="checked" name="patient[event_type_ids][]" id="patient_event_type_ids_1"> Bleeding puncture
<div data-target="patient_event_type_ids_1" class="form-sub">
  <div class="form-group">
    <input type="radio" value="true" name="patient[blood_aspirated]" id="patient_blood_aspirated_true"> Yes
    <input type="radio" value="false" checked="checked" name="patient[blood_aspirated]" id="patient_blood_aspirated_false"> No
  </div>
  <div class="form-group">
    <input type="radio" value="positive" name="patient[iv_test]" id="patient_iv_test_positive"> Positive
    <input type="radio" value="negative" checked="checked" name="patient[iv_test]" id="patient_iv_test_negative"> Negative
  </div>
</div>
<input type="checkbox" value="1" checked="checked" name="patient[event_type_ids][]" id="patient_event_type_ids_2"> Vascular puncture
<div data-target="patient_event_type_ids_2" class="form-sub">
  <div class="form-group">
    <input type="radio" value="true" name="patient[abc]" id="patient_abc_true"> Yes
    <input type="radio" value="false" checked="checked" name="patient[abc]" id="patient_abc_false"> No
  </div>
  <div class="form-group">
    <input type="radio" value="positive" name="patient[xyz]" id="patient_xyz_positive"> Positive
    <input type="radio" value="negative" checked="checked" name="patient[xyz]" id="patient_xyz_negative"> Negative
  </div>
</div>

// javascript
var subForms = $('.form-sub');

subForms.each(function() {
  var subForm = this;
  var parentForm = '#' + subForm.dataset.target;

  if ($(parentForm).prop('checked')) {
    $(subForm).show();
  } else {
    $(subForm).hide();
  }

  $(parentForm).change(function(){
    if($(this).prop("checked")) {
      $(subForm).show();
    } else {
      $(subForm).hide();
      $(':input')
        .not(':button, :submit, :reset, :hidden')
        .removeAttr('checked')
        .removeAttr('selected')
        .not(':checkbox, :radio, select')
        .val('');
    }
  });
});

我想清除所有input当复选框未选中时。线路$(':input')肯定是,但那是我应该选择响应的地方 input s 是 .form-sub 的 child .我如何在 Javascript 中做到这一点?

最佳答案

替换这一行

var parentForm = '#' + subForm.dataset.target;

var parentForm = $( subForm ).prev();

并将change事件更新为

 if ( parentForm.prop('checked')) {
    $(subForm).show();
  } else {
    $(subForm).hide();
  }

  parentForm.change(function(){
    if($(this).prop("checked")) {
      $(subForm).show();
    } else {
      $(subForm).find(':input')
        .not(':button, :submit, :reset, :hidden')
        .removeAttr('checked')
        .removeAttr('selected')
        .not(':checkbox, :radio, select')
        .val('');
      $(subForm).hide();
    }
  });

关于javascript - 在迭代中根据类选择子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34838562/

相关文章:

javascript - Webkit 和 Mozilla 改变滚动条

javascript - 不使用 jQuery 查找滚动条的垂直位置

javascript - 将项目添加到表格模板的 Handlebars

jQuery-pjax vs history.js 在点击时加载特定内容

javascript - 使用 JavaScript 替换一个字符上的多个实例

jQuery 包装链接

javascript - IE7 在某些机器上崩溃,调试栏中出现错误,但看不到 'showstopper'

javascript - 是否有媒体查询类型的方式来根据设备切换 js 文件?

javascript - 如何遍历一个json对象?

javascript - 使用 HTML 表单的请求正文发出 Post 请求