篮球: <input type="checkbox" name="box" value="篮球"/>
足球: <input type="checkbox" name="box" value="足球"/>
羽毛球: <input type="checkbox" name="box" value="羽毛球"/>
乒乓球: <input type="checkbox" name="box" value="乒乓球"/>
台球: <input type="checkbox" name="box" value="台球"/><br/>
<input type="button" value="one" οnclick="getCheckBoxValueOne()"><br/>
<input type="button" value="two" οnclick="getCheckBoxValueTwo()"><br/>
<input type="button" value="three" οnclick="getCheckBoxValueThree()"><br/>
function getCheckBoxValueOne() {
var ids = $('input:checkbox[name="box"]:checked');
var data = '';
alert(ids);
for (var i = 0; i < ids.length; i ++) {
data += ids[i].value + (i == ids.length - 1 ? '':',');
}
console.log(data);
}
function getCheckBoxValueTwo() {
var ids = $('input[type=checkbox]');
var data = '';
ids.each(function () {
if ($(this).prop("checked")) {
data = data + $(this).val() + ",";
}
});
data = data.substring(0, data.length - 1);
console.log(data);
}
function getCheckBoxValueThree() {
var data = $('input:checkbox[name="box"]:checked').map(function () {
return $(this).val();
}).get().join(",");
console.log(data);
}