同级复选框条件检查

时间:2017-03-07 04:39:29

标签: jquery html meteor siblings

复选框上的此Meteor模板事件click .vote会尝试检查它之前的直接兄弟,以查看它是否已被选中。怎么做到呢? THX

Template.checkbox.events({
  'click .valid': () => {
     //do stuff
  },
  'click .vote': () => {  //99c
    //check if its nearest sibiling is checked else exit
    if ($(this).prevAll('input.valid').checked) {  //<------ not cutting it
       console.log('its sibling is checked');
       //do stuff
    } else {
      console.log('its sibling is not checked');
      //do other stuff
    }
  }
});
<template name="checkbox">
  <div class="checkbox-container">
    <div class="checkbox">
      <label class="check">
        <input class="valid" type="checkbox" name={{name}} value={{value}} checked={{checked}}>{{label}}
        <input class="vote" type="checkbox" name={{name}} value={{value}} checked={{checked}}>
      </label>
    </div>
  </div>
</template>

2 个答案:

答案 0 :(得分:1)

试试这个:您可以使用下面的.siblings()

$(function(){
$(".vote").click(function(){
if($(this).siblings('input.valid').is(":checked")) {  //<------ not cutting it
   console.log('its sibling is checked');
   //do stuff
} else {
  console.log('its sibling is not checked');
  //do other stuff
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

    <div class="checkbox">
        <input class="valid" type="checkbox">Checkbox Valid
        <input class="vote" type="checkbox">Checkbox Vote
    </div>

有关 Siblings()

的详细信息

答案 1 :(得分:1)

&#13;
&#13;
$(".vote").change(function() {

  if ($(this).closest('.check').find('input.valid').is(":checked")) { 
    console.log('its sibling is checked');
    //do stuff
  } else {
    console.log('its sibling is not checked');
    //do other stuff
  }
}).change();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkbox-container">
  <div class="checkbox">
    <label class="check">
        <input class="valid" type="checkbox" name={{name}} value={{value}} checked={{checked}}>{{label}}
        <input class="vote" type="checkbox" name={{name}} value={{value}} checked={{checked}}>
      </label>
  </div>
</div>
&#13;
&#13;
&#13;

这是您正在寻找的解决方案:checked