如何检查是否已选中任何复选框

时间:2016-01-17 21:34:54

标签: jquery html checkbox

我想看看是否已选择特定div中的任何复选框,以便继续处理我正在处理的多阶段表单。目前是否选中了复选框,我收到警告" Tile selected"。

HTML代码

var tile;
function _(x){
    return document.getElementById(x);
}

function processStep1(){
    tile = _("tile").value;
    if(!("tiles_bg input:checked").length) {
    /*  _("step1").style.display = "none";
        _("step2").style.display = "block"; */
        alert(tile);
    } else {
        alert("Tiles selected");    
    }
}

jquery代码

// must be sorted by 'day'!!!
let arrA0 = [2.45, 2.75, 2.9, 3.1, 3.2, 3.3]
// associated values
let arrA1 = [145.0, 150.0, 160.0, 245.0, 250.0, 260.0]

let arr = Array(zip(arrA0, arrA1))

// now i have an array of tuples, where tuple.0 is key and tuple.1 etc. is associated value
// you can expand the tuple for as much associated values, as you want
print(arr)
// [(2.45, 145.0), (2.75, 150.0), (2.9, 160.0), (3.1, 245.0), (3.2, 250.0), (3.3, 260.0)]




// now i can perform my 'calculations' the most effective way
var res:[Int:(Double,Double)] = [:]
// sorted set of Int 'day' values
let set = Set(arr.map {Int($0.0)}).sort()
// for two int values the sort is redundant, but
// don't be depend on that!

print(set)
// [2, 3]
var g = 0
var j = 0
set.forEach { (i) -> () in
    var sum1 = 0.0
    var sum2 = 0.0
    var t = true
    while t && g < arr.count {
        let v1 = arr[g].0
        let v2 = arr[g].1
        t = i == Int(v1)
        if t {
            g++
            j++
        } else {
            break
        }
        sum1 += v1
        sum2 += v2
    }
    res[i] = (sum1 / Double(j), sum2 / Double(j))
    j = 0
}
print(res)
// [2: (2.7, 151.666666666667), 3: (3.2, 251.666666666667)]

2 个答案:

答案 0 :(得分:0)

我的解决方案是使用类而不是id:

($(&#34; .checkboxclass&#34;)。是(&#34;:已检查&#34;))警报(&#34;选择了瓷砖&#34;);

答案 1 :(得分:0)

我的建议是:

&#13;
&#13;
var tile;
function _(x){
  return document.getElementById(x);
}

function processStep1(){
  tile = _("tile").value;
  if(!$('#material1').find("input.select_all:checked").length) { // look for checked inside div
    /*  _("step1").style.display = "none";
                 _("step2").style.display = "block"; */
    alert(tile);
  } else {
    alert("Tiles selected");
  }
}
&#13;
<script src="http://code.jquery.com/jquery-1.11.3.js"></script>


<div class='material' id='material1'>
    <a class='product'><div class='tiles_bg' style='background:url(media/images/tile2.jpg);'><input class='select_all' id='tile' type='checkbox' name='1marm' value='Verde Guatemala'></div><p>Verde Guatemala</p></a>
    <a class='product'><div class='tiles_bg' style='background:url(media/images/tile1.jpg);'><input class='select_all' id='tile' type='checkbox' name='1marm' value='Bianco Carrara'></div><p>Bianco Carrara</p></a>
</div>

<button type="button" class="nasta" onClick="processStep1()">Nasta</button>
&#13;
&#13;
&#13;