迭代JavaScript中的所有复选框 [英] Iterate all checkbox in JavaScript

查看:84
本文介绍了迭代JavaScript中的所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试遍历所有复选框并检查是否已选中时,我遇到了一些困难.这是代码:

I am having some difficulties when trying to iterate all checkbox and check if it is checked. Here is the code:

content = "<table class=\"filter-table\">";
content += "<tr><td><input class=\"pssLabel\" type=\"checkbox\" onclick=\"toggleOverlayer('pssLabel')\">Show Label</td></tr>" + 
            "<tr><td><div id=\"pss\"></div></td></tr>";
content += "<tr><td colspan=\"2\" style=\"padding:5px;font-size:15px;color:black;\">Development Type</td></tr>";
content += "<tr><td><input value='Commercial and Residential' class=\"pssCheckBox\"  type=\"checkbox\" onclick='queryPSS()' >Commercial and Residential</td><td><input value='Commercial' class=\"pssCheckBox\"  type=\"checkbox\" onclick='queryPSS()' >Commercial</td></tr>";
content += "<tr><td><input value='Heavy Vehicle Park' class=\"pssCheckBox\"  type=\"checkbox\" onclick='queryPSS()'>Heavy Vehicle Park</td><td><input value='Hospital' class=\"pssCheckBox\"  type=\"checkbox\" onclick='queryPSS()'>Hospital</td></tr>";
content += "<tr><td><input value='Hotel' class=\"pssCheckBox\"  type=\"checkbox\" onclick='queryPSS()'>Hotel</td><td><input value='Industrial' class=\"pssCheckBox\"  type=\"checkbox\" onclick='queryPSS()'>Industrial</td></tr>";
content += "<tr><td><input value='Industrial-White' class=\"pssCheckBox\"  type=\"checkbox\" onclick='queryPSS()'>Industrial-White</td><td><input value='Office' class=\"pssCheckBox\"  type=\"checkbox\" onclick='queryPSS()'>Office</td></tr>";

这是我迭代所有复选框并执行检查的方式:

Here is how I iterate all checkbox and perform checking:

function queryPSS() {
var type_filter = new Array();

//Iterate thru all checkbox
$(":checkbox").each(function(index, element) {
    if($(this).is(':checked'))       
    {
        type_filter.push($(this).val());            
    }
});

}

但是,有什么方法可以跳过第一个复选框(无论是否选中),并检查其他复选框并存储到数组中.

However, is there any way for me to skip the first checkbox (no matter checked or not) and check for other checkbox and store into array.

谢谢.

推荐答案

由于您将pssLabel类分配给了第一个输入,因此您可以跳过该类的复选框:

Since you assigned pssLabel class to your first input, you can skip the check box with that class:

$(":checkbox").each(function(index, element) {
    if($(this).hasClass('pssLabel')) return;

    if($(this).is(':checked'))       
    {
        type_filter.push($(this).val());            
    }
});

这篇关于迭代JavaScript中的所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆