如何检查除特殊复选框以外的所有复选框?

时间:2019-04-15 10:29:30

标签: javascript jquery

如果我要检查除this way以外的所有项目,那么我想检查,但是我想检查不止一项,例如:

    <dependencies>
      <module name="javax.api"/>
      <module name="org.apache.commons.logging"/>
      <module name="org.apache.commons.collections"/>
      <module name="org.apache.commons.digester"/>
      <module name="org.apache.commons.beanutils"/>
      <module name="net.sf.jasper.reports.fonts" export="true"/>
      <module name="com.lowagie.itext"/>
      <module name="other.fonts.arial"/>
      <module name="other.fonts.times"/>
      <module name="org.jfree.chart"/>
      <module name="org.jboss.as.web">
        <imports>
          <include-set>
            <path name="org/eclipse/jdt/internal/compiler" />
          </include-set>
        </imports>
      </module>
    </dependencies>

然后此代码不起作用:

var ingore_ids = [1,2,3];

我该如何解决?

2 个答案:

答案 0 :(得分:3)

您的逻辑问题是SELECT oppreg.`oppreg_id`, `title`,`description`, activity_id FROM `oppreg` INNER JOIN oppreg2activity ON oppreg.oppreg_id = oppreg2activity.oppreg_id where oppreg.end_date >= CURDATE() GROUP BY oppreg.`oppreg_id`, `title`,`description`, activity_id having count(*)>1 希望选择器或元素数组从当前集合中排除。

要解决此问题,您可以使用not()从数组中包含的join()值构建选择器,如下所示:

id
var ingore_ids = [1, 2, 3];

$('#checkAll').click(function() {
  $('input:checkbox').not('#' + ingore_ids.join(',#')).prop('checked', this.checked);
});

一种替代方法是为<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label> <input type="checkbox" id="checkAll" /> check some...<br /> </label> <input type="checkbox" id="1" /><br /> <input type="checkbox" id="2" /><br /> <input type="checkbox" id="3" /><br /> <input type="checkbox" id="4" /><br /> <input type="checkbox" id="5" /><br /> <input type="checkbox" id="6" /><br />提供一个函数,该函数检查当前复选框的not()是否不在数组中:

id
var ingore_ids = [1,2,3];

$('#checkAll').click(function() {
  $('input:checkbox').not(function(i, el) {
    return ingore_ids.indexOf(parseInt(el.id, 10)) !== -1;
  }).prop('checked', this.checked);
});

答案 1 :(得分:1)

这是使用eachattr方法的另一种方式。

var ignores = [1,2,3];

    $('#checkAll').click(function() {
      $('input:checkbox').each(function(index, item) {

        var id = parseInt($(item).attr('id'));

        if(ignores.indexOf(id) == -1){

         $(item).prop('checked', 'checked')
        }
      });
    });

var ignores = [1,2,3];

$('#checkAll').click(function() {
  $('input:checkbox').each(function(index, item) {
   
    var id = parseInt($(item).attr('id'));
    
    if(ignores.indexOf(id) == -1){
     
     $(item).prop('checked', 'checked')
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>
  <input type="checkbox" id="checkAll" /> check some...<br />
</label>
<p>check all ignore 1,2,3</p>
<input type="checkbox" id="1" />1<br />
<input type="checkbox" id="2" />2<br />
<input type="checkbox" id="3" />3<br />
<input type="checkbox" id="4" />4<br />
<input type="checkbox" id="5" />5<br />
<input type="checkbox" id="6" />6<br />

相关问题