所以我有多个复选框。
<input type="checkbox" value="special" name="special_round[]">special1
<input type="checkbox" value="special" name="special_round[]">special2
<input type="checkbox" value="special" name="special_round[]">special3
<input type="checkbox" value="special" name="special_round[]">special4我只能推送已检查输入的值。
$('#click').on('click', function(e){
e.preventDefault();
e.stopPropagation();
var specialSequences = [];
$('input[name="special_round[]"]:checked').each(function(i, v) {
specialSequences.push($(v).val());
});
alert(specialSequences);
});如果我检查special1和special4,输出是
special,special我怎样才能做到这样,
special, 0 , 0, special //or
special, " ", " ", special
//or if i checked the special2 and speial4 the output should
" ", special , " " , special发布于 2016-09-03 13:12:17
您需要遍历所有复选框,并检查哪些复选框是“选中”的,哪些-不是。
使用以下方法:
...
var specialSequences = [];
$('input[name="special_round[]"]').each(function(i, v) {
var current = ($(v).is(":checked"))? $(v).val() : "";
specialSequences.push(current);
});https://jsfiddle.net/t6fL04cm/1/
关于.is() jQuery方法:https://api.jquery.com/is/
https://stackoverflow.com/questions/39307232
复制相似问题